Android close application on stand by - android

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.

Related

Closing running Application from BroadcastReceiver

In my App, there is a condition which check every day and if it gets true then I want my App get close in between the run like a crash and stack also gets clear .
I have try and tested many solutions but didn't find the one that works the way i wanted .
My BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
PreferenceForApp prefs = new PreferenceForApp(context);
Bundle bundle = intent.getExtras();
if (bundle!=null){
if(bundle.containsKey("exception")) {
// String e = bundle.getString("exception")
if(bundle.get("exception").toString().equalsIgnoreCase("http request failed with error_msg No Match Found")) {
prefs.setIsDeviceValidated(false);
prefs.setIsLogIn(false);
Log.i("Time", "Exception Occur");
Intent CSPIntent=new Intent(context,CSPLoginActivity.class);
CSPIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
CSPIntent.putExtra("close_activity", true);
Log.i("Time", "IntentExit");
context.startActivity(CSPIntent);
}
}
}
}
}
And code to finish in an Activity I am calling from broadcastReceiver:
if (getIntent().getBooleanExtra("close_activity",false)) {
Log.i("Time", "ExitCSPLogin");
this.finish();
}
This code is not closing App in between the run.
You need to register BroadcastReceiver in your activity and send broadcast to BroadcastReceiver when you want to close application.
In your Activity try this:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_CLOSE");;
BroadcastReceiver Receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
registerReceiver(Receiver, intentFilter);
in onDestroy() method of you Activity unregister BroadcastReceiver:
#Override
protected void onDestroy() {
unregisterReceiver(Receiver);
super.onDestroy();
}
Now when you want close application send broadcast to BroadcastReceiver:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_CLOSE");
sendBroadcast(broadcastIntent);
Hope this helps!
you have to check below condition in your app's mainActivity's onCreate method every time when user enter in your app. or in onResume if you want to to close your app immediately
if (!prefs.getIsDeviceValidated()) {
Log.i("Time", "ExitCSPLogin");
this.finish();
}
i assume you have more then one activity in your app, so insted of check above flag in every activity we 'll put it in main activity. allow user to use your app until he/she come at mainActivity
Note: create Broadcast Receiver for your App(add in manifest), not for specific activity

BroadcastReceiver Battery Change and FLAG_KEEP_SCREEN_ON flag ?

In my Main Activity i'm setting this flag onCreate :
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
But i want on to be able to turn it off on BroadcastReceiver
android.intent.action.ACTION_BATTERY_LOW"
How can i do this ? how will my main application know that the broadcast has been received and that it should turn off this flag ? .
You can register a BroadcastReceiver programmatically in your activity by calling registerReceiver() in onResume() and unregisterReceiver() in onPause():
// Placed at your activity level
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent);
if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction()) {
// Remove the flag
} else {
// Battery is okay, add the flag
}
}
public void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_LOW);
// Probably also want to receive notification that the battery has gone back up
filter.addAction(Intent.ACTION_BATTERY_OKAY);
registerReceiver(mBroadcastReceiver, filter);
}
public void onPause() {
unregisterReceiver(mBroadcastReceiver);
}

Android: BroadcastReceiver to know via code when the screen in on/off

I've a Broadcast receiver:
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//Do something
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent start=new Intent(context,MainActivity.class);
context.startActivity(start);
}
}
}
And, in my activity, into onCreate():
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
ScreenReceiver mReceiver=new ScreenReceiver();
registerReceiver(mReceiver, filter);
The problem is that, when my activity is displayed, the receiver performs correctly the action, but when it is in background, sometimes nothing happens.
What could be the issue?
Most likely, when your app goes into the background Android kills it to free up resources. Try starting a foreground service attached to an ongoing notification from your Activity, and register the BroadcastReceiver in that.

Display screen/activity on Unlock event?

I essentially want to display a screen whenever the screen is unlocked regardless of the application already running.
Can someone just tell me how to display text as soon as the phone gets unlocked. I can take it from then on.
I have the following code up till now which I found on the net....
Suppose I want to display abc.xml as soon as the phone gets unlocked. How will i add it in the ScreenReceiver Class ?
Also I do not want to set any screen when the application runs.Do I need to run the code below as service ?
public class SampleActivity extends Activity {
//Declare the necessary variables
private BroadcastReceiver mReceiver;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.v("$$$$$$", "In Method: onDestroy()");
if (mReceiver != null)
{
unregisterReceiver(mReceiver);
mReceiver = null;
}
}
}
where the Screen Reciever class is as follows
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF");
// onPause() will be called.
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
Log.v("$$$$$$", "In Method: ACTION_SCREEN_ON");
//onResume() will be called.
// Better check for whether the screen was already locked
//if locked, do not take any resuming action in onResume()
//Suggest you, not to take any resuming action here.
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
Log.v("$$$$$$", "In Method: ACTION_USER_PRESENT");
// Handle resuming events
}
}
}
For one, you don't display abc.xml, you display an activity, dialog, or other UI component. You can set up a broadcast receiver that listens for the ACTION_BOOT_COMPLETED intent. Once the device boot completes, you can start a sticky service to listen for the actions you have above. Presumably you'll want to display abc.xml in an activity, so you'll need to fire startActivity from one of the if() blocks above.

Unbinding a service when Android goes to Lock mode

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));

Categories

Resources