2016-04-27 14 views

答えて

0

サービスで、SystemPropertyをフェッチし、そのメソッドがLooper.loop()を呼び出すメソッドを作成できます。この実装は、これを行うための最適化された方法ではないかもしれないというループが時間 にSystemProperty時間をポーリングするように、それはアンドロイド4.4.2で使用されている、あなたはここでhttp://androidxref.com/4.4.2_r2/xref/frameworks/base/services/java/com/android/server/SystemServer.java あなたは上記のリンクで見ることができます見ることができます:

boolean disableStorage = SystemProperties.getBoolean("config.disable_storage", false); 
    boolean disableMedia = SystemProperties.getBoolean("config.disable_media", false); 
    boolean disableBluetooth = SystemProperties.getBoolean("config.disable_bluetooth", false); 
    boolean disableTelephony = SystemProperties.getBoolean("config.disable_telephony", false); 
    boolean disableLocation = SystemProperties.getBoolean("config.disable_location", false); 
    boolean disableSystemUI = SystemProperties.getBoolean("config.disable_systemui", false); 
    boolean disableNonCoreServices = SystemProperties.getBoolean("config.disable_noncore", false); 
    boolean disableNetwork = SystemProperties.getBoolean("config.disable_network", false); 

これらのブール変数は、Looper.loop()の助けを借りてinitAndLoop()メソッドでチェックされています。ここでは、単一のSystemPropertyであっても、変更があった場合に他のコンポーネントに通知することができます。何をしているかのコードを以下のhttps://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/SystemService.java

は、あなたがリンクの上で見ることができます:

もう一つの方法は、静的なコールバックを作成し、SystemPropertyのいずれかの変更を求める取得するには、ここにエコプリンティングのためのマスターブランチのコードを確認することです

private static Object sPropertyLock = new Object(); 

static { 
    SystemProperties.addChangeCallback(new Runnable() { 
     @Override 
     public void run() { 
      synchronized (sPropertyLock) { 
       sPropertyLock.notifyAll(); 
      } 
     } 
    }); 
} 

/** 
* Wait until given service has entered specific state. 
*/ 
public static void waitForState(String service, State state, long timeoutMillis) 
     throws TimeoutException { 
    final long endMillis = SystemClock.elapsedRealtime() + timeoutMillis; 
    while (true) { 
     synchronized (sPropertyLock) { 
      final State currentState = getState(service); 
      if (state.equals(currentState)) { 
       return; 
      } 

      if (SystemClock.elapsedRealtime() >= endMillis) { 
       throw new TimeoutException("Service " + service + " currently " + currentState 
         + "; waited " + timeoutMillis + "ms for " + state); 
      } 

      try { 
       sPropertyLock.wait(timeoutMillis); 
      } catch (InterruptedException e) { 
      } 
     } 
    } 
} 

/** 
* Wait until any of given services enters {@link State#STOPPED}. 
*/ 
public static void waitForAnyStopped(String... services) { 
    while (true) { 
     synchronized (sPropertyLock) { 
      for (String service : services) { 
       if (State.STOPPED.equals(getState(service))) { 
        return; 
       } 
      } 

      try { 
       sPropertyLock.wait(); 
      } catch (InterruptedException e) { 
      } 
     } 
    } 
} 

この情報はShridutt Kothariからのものです。 this Googleの投稿を確認して、単一のシステムプロパティの変更を聞くことについて

関連する問題