meta données pour cette page
Pilotage de ADB par Java avec ddmlib
Récuperer la ddmlib avec maven en ajoutant cette dépendance
<dependency> <groupId>com.google.android.tools</groupId> <artifactId>ddmlib</artifactId> <version>r13</version> </dependency>
Voici un bout de code permettant de lister les différents terminaux de deux manières différentes La première “usingWaitLoop” en attendant l'apparition d'un terminal avec une boucle. La deuxième “usingDeviceChangeListener” par gestion évènementielle (personnellement je préfère cette manière)
import com.android.ddmlib.AndroidDebugBridge; import com.android.ddmlib.IDevice; public class Demo { // you can call AndroidDebugBridge.init() and terminate() only once // createBridge() and disconnectBridge() can be called as many times as you want public void init() { AndroidDebugBridge.init(false); } public void finish() { AndroidDebugBridge.terminate(); } public void usingWaitLoop() throws Exception { System.out.println("Working Directory = " + System.getProperty("user.dir")); AndroidDebugBridge adb = AndroidDebugBridge.createBridge("adb/adb.exe",true); try { int trials = 10; while (trials > 0) { Thread.sleep(50); if (adb.isConnected()) { break; } trials--; } if (!adb.isConnected()) { System.out.println("Couldn't connect to ADB server"); return; } trials = 10; while (trials > 0) { Thread.sleep(50); if (adb.hasInitialDeviceList()) { break; } trials--; } if (!adb.hasInitialDeviceList()) { System.out.println("Couldn't list connected devices"); return; } for (IDevice device : adb.getDevices()) { System.out.println("- " + device.getSerialNumber()); } } finally { AndroidDebugBridge.disconnectBridge(); } } public void usingDeviceChangeListener() throws Exception { AndroidDebugBridge.addDeviceChangeListener(new AndroidDebugBridge.IDeviceChangeListener() { // this gets invoked on another thread, but you probably shouldn't count on it public void deviceConnected(IDevice device) { System.out.println("* " + device.getSerialNumber()); } public void deviceDisconnected(IDevice device) { } public void deviceChanged(IDevice device, int changeMask) { } }); AndroidDebugBridge adb = AndroidDebugBridge.createBridge("adb/adb.exe",true); Thread.sleep(1000); if (!adb.isConnected()) { System.out.println("Couldn't connect to ADB server"); } AndroidDebugBridge.disconnectBridge(); } public static void main(String[] args) throws Exception { Demo demo = new Demo(); demo.init(); // I think this is the way to go for non-interactive or short-running applications System.out.println("Demo using wait loop to ensure connection to ADB server and then enumerate devices synchronously"); demo.usingWaitLoop(); // this looks like the right way for interactive or long-running applications System.out.println("Demo using DeviceChangeListener to get information about devices asynchronously"); demo.usingDeviceChangeListener(); demo.finish(); } }
Source : https://github.com/Ladicek/ddmlib-demo/blob/master/src/main/java/cz/ladicek/android/ddmlib/Demo.java