i want to know that is there any way i can prevent my android app from killing from task manager. Whether it's any third party app, clears from ram manager or user clicks force stop. i just don't want kill my app from background, It should be running.
Asked
Active
Viewed 757 times
3
-
Possible duplicate of [Android: keeping a background service alive (preventing process death)](http://stackoverflow.com/questions/3856767/android-keeping-a-background-service-alive-preventing-process-death) – Rohan Khude Sep 29 '16 at 06:55
-
I think the app will be killed sometimes even by the system.. But you can run a service background and it will work even app is killed.. Alarm manager can be implemented so that even if app is killed or in sleep mode, any specific task can be implemented.. [http://stackoverflow.com/questions/4459058/alarm-manager-example] – Karthik CP Sep 29 '16 at 06:55
-
Can you tell us why you want to do that? – Stephan Branczyk Sep 29 '16 at 07:13
2 Answers
2
How to disable the "Force Stop" button
Short answer: Use the Device Administration API.
How do I demonstrate that it works?
Yes, back to your job. Use the API link provided above and the Api Demos included in Google's sample collection to figure out how to integrate this into your app.
- Build the demo and run it on your device.
- Choose
API Demos->App->Device Admin->General->Enable admin.
- Choose Activate once the Device Administration API prompts you with its enabling screen.
- Exit the app and attempt to manage the app via your device's settings menu (specifics for this step varies by device).
- When viewing the Api Demo's "app info" screen, you should see both Force Stop and Uninstall are disabled.
How do I do this in my own app?
Review DeviceAdminSample.java in the Api Demos app for inspiration. You will need the following:
The following code is what brings up the activation screen:
// Launch the activity to have the user enable our admin.
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
mActivity.getString(R.string.add_admin_extra_app_text));
startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
However, there are a few other pieces you will need to get this to work:
- A broadcast receiver that derives from DeviceAdminReceiver.
- Entries in your manifest file that refer to the above broadcast receiver.
- Permissions in your manifest for using the Device Administrator API.
- An xml file stating what policies your app can access.
All of this can be found in the above links. Good luck with your client!

Jorn Rigter
- 745
- 1
- 6
- 25

Jai Mahlawat
- 47
- 1
- 11
-
That is very helpful, and that works . But can we somehow disable the task manager as well ? – Ace Sep 29 '16 at 08:11
0
This might be a dirty way to do this. But it worked for me.
Just override onDestroy()
method in service and start that service again.
@Override
public void onDestroy() {
Intent intent = new Intent(this,YourService.class);
startService(intent);
}

rehman_00001
- 1,299
- 1
- 15
- 28