我可以在android中编写系统属性侦听器吗? [英] Can I write a system property listener in android?

查看:95
本文介绍了我可以在android中编写系统属性侦听器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经使用setprop命令(通过adb)在android中设置了系统属性,有没有办法在我自己的服务中侦听此更改?

Having set a system property in android using the setprop command (through adb) is there a way to listen to this change in my own service?

我尝试使用SystemProperties.addChangeCallback,但未收到通知.有什么我想念的吗?

I tried with SystemProperties.addChangeCallback and was not notified. Was there something that I missed?

推荐答案

您可以在服务中创建一个方法,该方法应获取所有Systemproperty,并且该方法应调用Looper.loop();.这样该循环将不时轮询SystemProperty此实现可能未经过优化,但是已在Android 4.4.2中使用,您可以在此处查看

You can create a method in your service which should fetch any Systemproperty and that method should call Looper.loop(); so that that loop will poll for SystemProperty time to time This implementation may not be optimized way of doing this but it is used in Android 4.4.2, you can see here http://androidxref.com/4.4.2_r2/xref/frameworks/base/services/java/com/android/server/SystemServer.java you can see at above link:

    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中将任何更改通知其他组件.

These boolean variables are being checked in initAndLoop() method with the help of Looper.loop(); here you can notify your other components on any change in even a single SystemProperty.

另一种方法是创建静态回调并获取SystemProperty中任何更改的调用,请在此处查看SystemService的master分支代码:

Another way is to create static callback and get call for any change in any of SystemProperty, see the master branch's code for SystemService here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/SystemService.java

您可以在上面的链接中看到以下代码在做什么:

you can see in above link what following code is doing:

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.查看 Google帖子,其中涉及收听单个SystemProperty变化

This information originates from Shridutt Kothari. Check this google post about listening to single SystemProperty changes

这篇关于我可以在android中编写系统属性侦听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆