Android moveTaskToFront Service - android

Android 4.0 which is now allow the developer to disable the application.
Then I developed a service to bring to application to foreground when the user click the home button.
Now I can bring the application to foreground successful.
But each time I opened the application and then click home button.
it will show the launcher and delay 5-6 second to bring to application to foreground.
I find that the service retried a few time to bring the application to foreground.
But when I click the button on the launcher, it will bring the application to foreground immediately.
Any solution to fix the delay problem?
Thanks.

Related

Bring app to foreground from background, Xamarin Android

I have an android app with a single activity which is launched with SingleInstance mode. The app has a countdown timer. When the app is minimized and goes to the background and other apps are used, I want to bring my app to front when the countdown timer finishes.
I tried:
StartActivity(Intent.AddFlags(ActivityFlags.ReorderToFront));
But it does not bring my app to front. However, it may reactivate another app which is at the top of all minimized apps.
I have tried other ActivityFlags but they don't bring my app to front when it is minimized and another app is used. How can I bring the minimized app to front when other apps are used?

How to reset/restart the app when onTrimMemory() method is called?

Currently I am handling onTrimMethod(int level) in my app with ComponentCallbacks2.TRIM_MEMORY_COMPLETE and ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL. I get this callback easily with low end devices when my app is running in background and simultaneously I am playing games.
In that I put Runtime.getRuntime().exit(0); to kill the app in background only so that when user taps on app (running in background), app will restart from launcher activity. I don't know it kills the app or not but when I open the app, first it shows dialog with "Unfortunately, app has stopped" and app does not restarts.
If I start the activity from this method, it will launch the application and bring it to foreground without any user interaction.
I tried some other ways too to kill the app. They are System.exit(0); and android.os.Process.killProcess(android.os.Process.myPid()); but none of them are solving my problem.
So, what to write in onTrimMethod() or how to handle in other way so that when user opens the application (brings to foreground from background) it will start from the first activity?

Launch android app after start and block other apps

I'm facing the following problem. I want to make an android device to run only my application. All other apps and phone feautes should not be available to a user.
The reason why I want to achieve this is simple: I want to destribute devices with preinstalled application to my client but I don't want to let them use all phone featues.
This could work this way: just after android boots my application is launched automatically and than somehow all other staff is blocked.
Do you have any suggestions how to achieve that? Is it possible? Do I need to root a device?
I hope you get my problem. Any advice you can give will be greatly appreciated.
This is a bit crude way. But see if it is of any help.
If you create your application as a launcher it will start on boot(using system broadcast BOOT_COMPLETED).
Then you need to override all the three android buttons Home, back and recent apps.
To override back button you just have to override the onBackPressed() method.
For home button you will start a service which will run in background. This service will check if your app is in foreground or not. if not then it will bring it in foreground. so when the user presses home the service will sense the foreground app isnt yours and launch it.
But for this switching back of your app android takes approx 3 to 5sec. In that period of time you can display a warning text which will block the user from stopping the service from settings.Also if you remove the activity animations then it will appear seamless.
For recent apps button the above trick will do. If the user tries to close the app using this button your background service will sense it and launch your app.
so its the background service that does all the trick. if you some how stop the service or uninstall the app you are through :p

How to bring an application to front of another one which is currently running?

I Have an android application in which I handled the device Home button click.I made the application relaunched automatically when user clicks on Home button while my application is running using Service.
When user clicks on Home button while running my application , shows default home screen and then shows again the activity.
But my problem is that is takes some long time to show last activity of my application from home screen.ie,the home screen is displayed for a long time.Because of this user can launch any other application from home screen and my application runs in background.ie,When pressing BACk button after launching another application it is possible to view my application.
Is it possible to avoid this delay and bring my application to front of that launched app?
Thanks in Advance
Place the Home button Code in your AsyncTask doinBackground() or you can use Handler. That would help you in reducing the delay of loading the activity. For more information on How to use AsyncTask and Handler, check this link: Android Background Processing
I implement a countdown timer in my service that repeatedly start my activity in onStick. When my activity comes to the foreground, in onNewIntent I send a message to the service to cancel the countdown timer. It works out pretty well.

Using home button on android phone

In iphone when we exit app and start app again by clicking launcher icon the app start from screen which was open last time before exiting app.
We can achieve same in android when we exit app using center or home button on android phone. In android app if app is exited by pressing home button and started again by clicking on launcher icon the app start from activity which was open last time before exiting app.
Is that functionality similar to iphone in terms of keeping app in memory longer? Will that work in android all time because I think android system will remove app from memory after sometime or will it remain in memory longer? Can I depend on this functionality in android and expect it to work all time.
I have a project in android which client has asked me to have iphone like functionality if we exit app and start app again it should open from screen which was opened last time before exiting. Now this is possible in android only if we use centre button. And also if user has exited using centre button and started it again, app should check for user current location and do some other operations. If app is launched and user navigate to any activity is there any way to check if app was exited using back button or centre button so that I can run code if app is exited using center button.
Thanks
We can achieve same in android when we exit app using center or home button on android phone.
The HOME button does not "exit app".
The HOME button brings the home screen to the foreground, just as the CAMERA button (where available) brings the camera application to the foreground, the CALL button (where available) brings the dialer to the foreground, tapping on a Notification may bring something else to the foreground (e.g., SMS client) based on the Notification, etc.
In android app if app is exited by pressing home button and started again by clicking on launcher icon the app start from activity which was open last time before exiting app.
No. If you tap on a launcher icon, and the app's process is still in memory, the existing app instance will be brought to the foreground, returning you to whatever activity you had been on. If you tap on a launcher icon, and the app's process had been terminated to free up RAM for other apps, you launch a fresh copy of the app and bring up whatever the ACTION_MAIN/CATEGORY_LAUNCHER activity was that the user tapped upon.
Will that work in android all time because I think android system will remove app from memory after sometime or will it remain in memory longer?
The length of time that a non-foreground app's process will be in memory is indeterminate and will be based on what is going on with the device, plus the device capabilities (e.g., how much RAM). I suggest you read more about the process lifecycle.
Now this is possible in android only if we use centre button.
It is not possible "if we use centre button" (what Android developers refer to as the HOME button). It may happen automatically, but if the app's process has been terminated, it will not happen automatically.
If app is launched and user navigate to any activity is there any way to check if app was exited using back button or centre button so that I can run code if app is exited using center button.
You should not care whether the "app was exited using back button or centre button".
You should care whether you have your data and how old that data is, refreshing it if it is stale.
Whether the user left your app via HOME, BACK, CAMERA, CALL, a Notification, the recent tasks list, an incoming phone call, by smashing their phone to bits with a rock and replacing it with an exact duplicate, or by any other means, should not matter to you.
To draw an analogy, think of a Web app. In a Web app, you care about whether you have a session cookie and whether that session is stale (e.g., to force a fresh login). Whether the page request came because the user clicked a link within the app, or clicked on a link from a third party site pointing to your app, or refreshed their page, or used a bookmark to get at another page in your app, or double-clicked on a desktop icon that brings up your app, or right-clicked on a link and opened a fresh tab, or anything else, should not matter to you.
Review the Android Activity Lifecycle at http://developer.android.com/reference/android/app/Activity.html. If you do nothing, you have no guarantee of your app starting at the same point it left off, however you have control here. You can, for example, overload the onPause() method to save your state to a file, and onResume() to restore it.

Categories

Resources