Android, How to make the task of the app unclosable? Only closable by task killing

我只是一个虾纸丫 提交于 2019-12-04 17:45:14

Write a service. Your app communicates with the service. When your app exits, the service remains running.

http://developer.android.com/reference/android/app/Service.html

I'm developing an app that have to be always running and be only closable with a task killer or similar.

That is not strictly possible. Android can and will eventually get rid of your process, to prevent sloppy developers from trying to have "an app that have to be always running".

If your application is part of the foreground user experience (e.g., music player), you can have a service that calls startForeground(), which reduces the odds that Android will get rid of your process, so long as there is a Notification in the status bar associated with your app. Your app still will get its process terminated eventually, though it will tend to take longer.

Or, you can make your own custom build of the Android operating system that contains a C daemon that implements your logic, and distribute a ROM mod containing your customized Android.

To have your app continue running after you close it, you need to make a service inside you app to do whatever you want to happen in the background.

If you are new to developing Android apps, you might want to take a look at the following tutorial for Android services: http://www.vogella.com/articles/AndroidServices/article.html. In addition, I have found that the Android documentation is very helpful. I would suggest you read both the API Guide for Services and the API Reference for the Service class.

Use a combination of Service, AlarmManager, and BroadcastReceiver. The AlarmManager issues a pulse, the BCReceiver catches and starts the service every so often as needed. This will essentially keep the service alive endlessly since Android will see that it periodically gets used. Technically the lifecylce of your app, service is under androids control, but you can start the service sticky, which helps and periodically call it. AlarmManager is extremely reliable.

If you need to run at all times, look into a Service and startForeground. If you can let your Service die but get restarted, look into onStartCommand and START_STICKY.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!