android


Can I write a system property listener in 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 ?
I tried with SystemProperties.addChangeCallback and was not notified. Was there something that I missed ?
Thanks in advance.
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);
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.
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) {
}
}
}
}
This information originates from Shridutt Kothari. Check this google post about listening to single SystemProperty changes

Related Links

how to get text files listed in listview in Android
Android FAB drag and drop
app stopped working, httpClient [duplicate]
How to capture Modem Radio Log in Android
How to detect the view when touch move from one layout to another
PlaceAutocomplete search pause until user typing from softkeyboard(Android)
Intent initialization on android
Android Background service and process
android: How to give animation to view or edittext as like below image?
how to set focused on searchview when Activity are start?
how to align tableview column to the largest value text value in adjecent column
getLastKnownLocation() returns null without tab
Recycler view with Firebase data only loads when scrolling
How to check whether app is compatible in all devices or not?
Instrumentation Testing WebView
Video Upload issue in android

Categories

HOME
grok
jpa
ecmascript-6
depth
devise
switch-statement
vsm
vert.x
discrete-mathematics
resultset
mxgraph
gource
html5-appcache
cfml
symbols
ag
perlbrew
jmockit
gojs
hibernate-search
baidu-map
processwire
bc
comparison-operators
plaintext
contract
datamaps
static-cast
oracleclient
payload
cmp
vibrate
android-instrumentation
donations
chromium-os
custom-lists
redistributable
tinkerpop3
greatest-n-per-group
firebase-security
observablecollection
jtwig
bizagi
cda
thread-sleep
false-sharing
crouton-os
character-replacement
concurrentmodification
poppler
rpart
cwrsync
telepat
osc
cakephp-2.7
umlgraph
gamekit
procedural-programming
crowd
psr-4
universal-image-loader
raygun
template-deduction
multitasking
script#
nebula
process-explorer
video-codecs
dopostback
infobright
codebase
cac
load-time
blender-2.49
build-environment
web-application-project
mathematical-notation
sun
google-wave
cldc
dojo-dnd
fault-tolerance
sqlobject
xslcompiledtransform
movieplayer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App