我可以在android中编写系统属性监听器吗?

时间:2016-04-27 10:19:23

标签: android listener system-properties

使用setprop命令(通过adb)在android中设置系统属性是否可以在我自己的服务中监听此更改?

我尝试使用SystemProperties.addChangeCallback但未收到通知。有没有我错过的东西?

1 个答案:

答案 0 :(得分:0)

您可以在服务中创建一个应该获取任何Systemproperty的方法,该方法应该调用Looper.loop();这样该循环将不时轮询SystemProperty     此实现可能不是优化的方式,但它在Android 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中通知您的其他组件。

另一种方法是创建静态回调并调用任何SystemProperty中的任何更改,请在此处查看SystemService的主分支代码:https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/SystemService.java

您可以在上面的链接中看到以下代码正在执行的操作:

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。查看有关收听单个SystemProperty更改的this google帖子