Am writing an android application which in background will monitor the device.I want to get an indication whenever an application moves to background/foreground.Also 开发者_如何学JAVAI want to get an indication when an application installed or gets uninstalled from the android device. how can this be acheived in android ?please help .thank you
For the app moving to the background/foreground part of the question: AFAIK there is no way to do this for apps other than your own, which is what I think you are wanting here.
For the install/uninstall detection, you'll need to register a BroadcastReceiver in your app to receive the ACTION_PACKAGE_ADDED & ACTION_PACKAGE_REMOVED intents. Something like:
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Your implementation
}
};
registerReceiver(br, new IntentFilter(Intent.ACTION_PACKAGE_ADDED));
registerReceiver(br, new IntentFilter(Intent.ACTION_PACKAGE_REMOVED));
For the foreground/background part of your question have a look at the "Activity Lifecycle".
For notifications about installs and uninstalls you can register a BroadcastReceiver
for the events Intent.ACTION_PACKAGE_ADDED
and Intent.ACTION_PACKAGE_REMOVED
Something like
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
// do something
}
}, new IntentFilter(Intent.ACTION_PACKAGE_ADDED));
精彩评论