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 provide different android widget for 1.6 and 3.0+?
How can I filter out a folder(not load contents from it) so that it's content's do not show up in my ListView?
App not deploying in AVD via Eclipse?
Remove unwanted spacing between lines in TextView
Android notification with buttons on it
Is wakelock needed in broadcast receiver for ACTION_SCREEN_OFF
Android There is a way to merge/glue/stick couple of wav sounds together?
Android API 7 (2.1), MapView, MultiThreading, ConcurrentModificationException
Returning jbyteArray from native c in Android
Is there a way to find out connectivity possibilities?
How to use SOAP Service android [closed]
how to detect whether the function has run finished?
multicastlock not working on Android 2.2 phone
If I'm setting the image for a button, where would I need to save the image in Eclipse?
Android PagerAdapter not calling instantiateItem
Populating listview in separate thread

Categories

HOME
gridview
ng-idle
c99
apache-flink
github-for-windows
youtube-data-api-v3
apache-spark-mllib
yahoo-finance
apiblueprint
knitr
yandex
xbee
telegram-bot
frontend
joomla3.0
arangodb
reactive-programming
java.util.logging
cmdb
pybossa
flash-player
keytool
searchview
microsoft-dynamics-nav
swagger-2.0
simpleitk
blade
easeljs
gmock
froala
custom-post-type
corenlp-server
mockjax
cognos-bi
hunspell
jwplayer6
xajax
engine.io
latitude-longitude
parse-tree
salesforce-communities
diacritics
hackintosh
tcc
plan-9
pom.xml
openrasta
gitlist
dbcontext
amq
modelsim
compiler-design
edit
jqchart
quicktime
mathcad
vibrate
pytables
async.js
vulcanize
observablecollection
android-wake-lock
jparsec
piracy-prevention
avisynth
fiware-monitoring
lazybones
rate-limiting
typemock
liquid-layout
typeconverter
snackbar
delayed-execution
android-vibration
ghcjs
sip-server
webfinger
gii
mintty
gnumeric
renderer
cfcache
octal
correlated-subquery
subproject
promotion-code
nhprof
office-2007
ajax-polling
folding
qtabbar
wcf-instancing
cldc
escrow
product-management
netbeans6.1

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