As #hackbod stated here, I used the onStart/onStop couple to bind/unbind to my service.
I want my service to stop running when in lock mode (to save battery) and to resume when coming back. However, Lock mode works with the onResume/onPause couple. So how do I do that?
Thanks
You can listen for broadcast when the screen turns on/off.
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// bind to service
}
}, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// unbind from service
}
}, new IntentFilter(Intent.ACTION_SCREEN_OFF));
Related
i want to unregister some broadcast receivers with single click.here is the flow.. lets say in Activity A i have below broadcast receivers.
public BroadcastReceiver upload = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
public BroadcastReceiver download = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
and in Activity B i have below broadcast receivers
public BroadcastReceiver wifi = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
public BroadcastReceiver data = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
my problem is i want to unregister all these BroadcastReceiver from Activity C with a button click. How can i do that? and how can i check is receiver is registered or not..?
Keeping broadcast receiver register even after activity is out of screen is memory leak and you should not do that.
Always register your broadcast receivers in onStart/onCreate/onResume and unregister them in onStop/onDestroy/onPause.
Why do you need to keep receiver active even in case activity is out of screen? You might as well use Android Service if you want something to execute out of activity scopes.
Create an Interface in your Activity C with a method unresisterRegisters()
Implement this Interface In Activity A and B. Overrrid the method and write code for unregistere Receivers
create object of A and B in Activity C inside OnButton click and call unresisterRegisters() method with both methods.
I hope this will help You.
How can I close my application when the Android goes to stand by either by user pressing the button or screen timeout?
I've tried onResume(), onPause and onRestart() methods but I already use then in another context.
Regards
In your application class onCreate() method register receiver for Screen Off Event
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d(TAG, Intent.ACTION_SCREEN_OFF);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d(TAG, Intent.ACTION_SCREEN_ON);
}
}
}, intentFilter);
like this.
I am new to Android development.
I want to get a notice when the operating system (because of lack of memory) or other application (Task killer e.g. ZDbox) restart other applicatoin (not mine).
I tried BroadcastReceiver. It did not get any Intent when an app was killed and the user started it manually. I have used PACKAGE_RESTARTED intent but BroadcastReceiver did not get any Intent.
Any suggestions?
public class MyService extends Service {
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
filter.addDataScheme("package");
BroadcastReceiver pkgRemoveReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do my stuff
}
};
registerReceiver(pkgRemoveReceiver, filter);
}
I've a local service that is started in my main activity. The service is responsible for network I/O. It also interacts with other activities in my application. Till, now the service was only "providing" data to activity (using callbacks and not Intents.)
Now I need to send data (custom objects) from my activities to the service. I understand one way to do it using Intents. I know it is possible to receive an Intent in the service by overriding the onStartCommand() in my service. But it's not clear to me if the onStartCommand will be invoked every time I broadcast an Intent from my sending activity. Can a Service also be BroadcastReceiver ? If yes - how ?
Thanks.
You can create a BroadcastReceiver object in the service and register it to listen to any broadcast event you want. It's something like this:
BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//handle the broadcast event here
}
};
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mScreenStateReceiver, filter);
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mScreenStateReceiver);
}
Regards,
I have the following broadcast receiver:
public class MyRingModeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Logger.iLog("In my Receiver");
}
}
I then have a service that onCreate does the following:
IntentFilter filter = new IntentFilter();
filter.addAction("android.media.RINGER_MODE_CHANGED");
registerReceiver(new MyRingModeReceiver() , filter);
When I place a call to the emulator and use the volume keys to modify (silence) the ringer
nothing happens. Any ideas of what it is that i am doing wrong?
Thanks.
Apparently, the Intent Ringer_MODE_CHANGED is not called when you silence a ringer using the phone app ... Only when you modify the ringer outside of a phone call.