There is a Xamarin app that I didn't develop and I don't have the source code for.
It's important that android OS will not kill or put this app to sleep if the user didn't use it for a 2-3 hours.
Can I develop another small app or service that will help keeping this app alive?
I already red online articles and tried changing battery optimization settings. Nothing worked.
there are no methods in API for manipulating other apps lifecycle, system decides when it is needed to kill some app. maybe you can introduce some own custom AccessibilityService, which will start this target app foreground (simplest way: open launcher Activity, just start app as usual):
Intent intent = context.getPackageManager().getLaunchIntentForPackage("target.app.package");
startActivity(intent);
and then after few seconds "home" it using below
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
note that this operation will be visible for user
Related
I want to add "exit" functionality.
Display.getInstance().exitApplication(); almost works, but leaves
behind a "ghost" window in the android open applications view.
Why don't you finish all Activities and Services, and set every other reference to null.
That would leave you with just the Application Singleton alive if you have one.
Every Application on android OS, is like a User in Linux OS, and every application runs in a process which has a Process ID assigned to it which application does not know, but we if we kill the process and entire application will stop.
Here is how you can do.
android.os.Process.killProcess(android.os.Process.myPid());
but this would restart your first activity of your application, as android will try to recover the Application, you can avoid that by going to home screen like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Display.exitAppliction is the only way to exit an app but it won't be removed from the task switcher. Thanks to the comment from #david-medenjak I was able to find this:
android:noHistory="true"
To use that just use:
android.xactivity=android:noHistory="true"
In your build hints and the app won't be in history at all. But as people said this is a pretty bad practice...
FYI in iOS if you include an exit button your app will be rejected by review unless its a "logoff" button. Typical mobile apps never really exit and are only supposed to exit for security purposes.
The OS will kill your app if it needs the resources. Notice that this is important since mobile apps are stopped/restarted often e.g. incoming phone call.
My app requires location services to be enabled.
I wrote a loader Activity that verifies the different providers, and launches the system settings interface if something needs reconfiguring, much like Google Maps does.
Intent goToSettings = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(goToSettings);
Problem is, if the user navigates away without finishing the settings panel, it's left at the top of the Activity stack and shows up again when the app is brought to foreground (on relaunch, maybe in other scenarios).
How can I "detach" the system settings from my app? I tried combining flags and launch modes, but I can't get it working.
Use intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to launch the external intent as a separate task.
To exit an app programatically in Android (for instance, if the user presses an exit button), I use:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
However, the CATEGORY_HOME intent category is not supported when porting Android apps for Playbook or Blackberry 10. What should I use instead?
I think your "exit an app code" for your Android app is quite the opposite of exiting the app: You start a new activity for the home screen instead of finishing "your own" activity.
You should instead finish your activity like this (and consider if that applies to more than one activity on your stack):
myAppView.finish()
This would also "quit" your application on bb10 - which means, it will be minimized and shown as an active frame until you tip on the X to close it.
As well, it would be a good idea to decide if you should clear outstanding notifications at this time (for my app, this makes sense...)
i know that this question is asked many times, but really i can't understand the answer, i want to set button, when user click it i want to exit the application (also the carbage collector should remove the objects), and after exiting i want to go to the screen where the user found the application icon on mobile.
for exit i don't know what to do
for going to the screen where to find the application icon i tried like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but doesn't work
use this method, this will make your application to go in backgroud and will open home/app_menu screen
moveTaskToBack(true); // method available in activity
reference link
When you start that activity, it will effectively stop your application's foreground activities. Android is designed to NOT shutdown the app so that it may be restarted faster.
However, if you really really want to purge the app from memory, then what you want to do is kill exit() on the Runtime singleton : http://developer.android.com/reference/java/lang/Runtime.html#exit(int)
My application is running normally and when I move on some new activity I used the code as
Intent start=new Intent(current activity.this,new activity.class);
startActivityForResult(start, 0);
finish();
and then when I have to move back to the earlier activity I used the following code
Intent start = new Intent(current activity.this,earlier activity.class);
startActivity(start);
finishActivity(0);
I think in this way the stack of the activities may be clear. And on click of the button or the phone back button I used the following code
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
But my application is still running in the background. I also used the android:noHistory="true" app is killed from the background but message appered that app close forcefully. Simple finish(); moves to the last activity.
I also seen the link Quitting an application - is that frowned upon? but I am new so did't get much. Please suggest me some way how can I deal with this.
I think in this way the stack of the activities may be clear
You are welcome to your opinion. startActivityForResult() is not designed for how you are using it.
And on click of the button or the phone back button I used the following code
I would fire anyone who worked for me who did this.
Please follow the Android design guidelines for proper handling of the BACK button -- while I do not agree with everything in there, it is much better than what you are doing. In the eyes of the user, BACK means back.
But my application is still running in the background
So long as you have not leaked any threads or services, your process will be running, but it will be consuming no significant CPU time. Android will terminate the process when it needs the RAM. This way, if the user elects to return to your application, or something else in your application needs to run (e.g., AlarmManager event, broadcast Intent), Android does not have to waste CPU time and battery life to fork another process.
Please suggest me some way how can I deal with this.
Just leave the process alone, and nobody will get hurt.