2011-04-06 2 views
1

Je développe un programme qui permet à l'utilisateur d'afficher sur son écran d'ordinateur l'écran actuel de son téléphone Android (connecté à l'ordinateur via USB), grâce à l'utilisation du ddmlib bibliothèque. J'ai besoin de savoir si le téléphone est en mode paysage ou portrait.Obtenir une orientation de l'appareil Android en utilisant ddmlib.jar

Existe-t-il un moyen de vérifier cette valeur à partir d'un programme exécuté sur l'ordinateur?

Répondre

1

Voici ma fonction pour l'obtenir mais c'est assez lent. Pour l'instant je cherche une solution plus rapide.

private int getLandscape() { 
     int result = 0; 
     try { 
      String[] commands = new String[] { "adb", "-s", actualSerial, 
        "shell", "dumpsys input | grep 'SurfaceOrientation'" }; 
      Runtime rt = Runtime.getRuntime(); 
      Process pr; 
      pr = rt.exec(commands); 
      BufferedReader input = new BufferedReader(new InputStreamReader(
        pr.getInputStream())); 
      String line = ""; 
      line = input.readLine(); 
      if (line != null) { 
       String[] lines = line.split(":"); 
       result = Integer.parseInt(lines[1].trim()); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return result; 
    } 

La partie la plus lente consiste à lire dans BufferedReader.

0
class AReceiver implements IShellOutputReceiver { 
    int x; 
    int y; 
    @Override 
    public void addOutput(byte[] arg0, int arg1, int arg2) { 
     String xS = new String(arg0); 
     xS = xS.substring(26).trim(); 
     String[] screenRes = xS.split("x"); 
     x = Integer.parseInt(screenRes[0]); 
     y = Integer.parseInt(screenRes[1]); 
    } 

    @Override 
    public void flush() { 

    } 

    @Override 
    public boolean isCancelled() { 
     return false; 
    }  
} 
AReceiver receiver = new AReceiver(); 
try { 
    dev.executeShellCommand("dumpsys window | grep \"mOverscanScreen\"", receiver); 
    res[0] = receiver.x; 
    res[1] = receiver.y; 
} catch (TimeoutException | AdbCommandRejectedException | ShellCommandUnresponsiveException | IOException e) { 
     e.printStackTrace(); 
} 

Solution Ffaster utilisant executeShellCommand. Sur ma machine la solution précédente prend environ 400ms pour obtenir la réponse et cette solution obtient le paysage dans environ 50ms :) Bonne chance avec votre application!

Questions connexes