Restart the service even if app is force-stopped and Keep running service in background even after closing the app How?

前端 未结 8 570
陌清茗
陌清茗 2020-11-30 02:24

I am trying to run a service in the background. What my app does is when user checks checkbox then service starts and when it is unchecked service is stopped. Which is worki

相关标签:
8条回答
  • 2020-11-30 02:29

    Let your Service run in a separate process. There is a good tutorial in this blog.

    You can specify the android:process attribute in <Service> tag to make your app run within its own process.

    <service
    android:name="MyServiceName"
    android:process="my_process" 
    android:icon="@drawable/icon"
    android:label="@string/service_name"
    >
    </service>
    
    0 讨论(0)
  • 2020-11-30 02:39

    You cannot prevent your service from being killed under all circumstances. However, you can ask the system to restart it. There are two cases: (1) the process dies for some abnormal reason (2) the phone reboots. In the former, START_STICKY or START_REDELIVER_INTENT are used to restart the service. In the latter, you'll need to add a BroadcastReceiver for android.intent.action.BOOT_COMPLETED.

    Your code is returning START_STICKY from onStartCommand, so you've chosen one of the service restart paths: "if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service..."

    "This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback."

    You can also use START_REDELIVER_INTENT.

    Note that if your service is doing any significant work, it needs to run in a background thread.

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

    0 讨论(0)
  • 2020-11-30 02:41

    I think this link will help you.
    Disable force stop button in manage application

    Its disable the Force Stop button in Package Installer setting so, no one can stop your application.

    0 讨论(0)
  • 2020-11-30 02:42

    I'm not entirely sure you can prevent your app from being closed by the TaskManager. If you think about it, it makes sense for it to be that way. Imagine that you have an app that fails to respond to user input and also fails to respond to being killed by the Task Manager. Not good. However I found this question which is in a similar vein to yours. Also you can have the system automatically re-start your Service as described here (scroll down on that page a little to just before 'starting a service'

    0 讨论(0)
  • 2020-11-30 02:46

    I am not sure which 'Task Manager' you are referring to as different ones would act differently, so I am basing my answer on the action when the user goes to Settings-->manage Applications and--> force stops the app the way android has given him.

    Assuming that your service is running as part of the process and if the user force-stops your process, you are prevented from ever running the service again until the user manually launches you.This is especially valid from 3.0 and above version ( check for yours). It also seems logical when you think that there is an app which keeps a service started all the time and is annoying the user in some way. So when the user orders a hit ( :) force-stops) on the app, it should not restart the service to continue bugging the user.

    For instance, Imagine what would happen if you could create apps which just ate at your processor time by holding a wake lock, and you couldn't kill them. This would be horrible and a huge security disaster.

    So, you will not be able to restart your service by any means until the user launches one of your activities.

    Also you cannot disable the force-stop button AFAIK. You should take the viewpoint that nothing on the device is yours to control besides your app and (to a limited extent) the resources to which you're granted access.

    Finnally, even the gtalk app will bend to your will if you desire to force stop. It will start only when you use Gtalk or other apps which use the gtalk service such as PUSH Gmail ( for phones where gtalk isnt a part of firmware). Also take a look at Android C2DM here:

    https://stackoverflow.com/a/11238779/1218762

    0 讨论(0)
  • 2020-11-30 02:53

    But the problem is when I closed the app from task manager it then also stops the service.

    Correct.

    What I want is to keep the service running even after it is close from task manager. Then only way to stop that service is by user himself by unckecking the box.

    You can implement your service in C/C++, build it into some custom firmware, and convince people to install that firmware on their devices.

    When we install gtalk and once we sign in then it does not appear in active applications tab in task manager. But it runs in the background.

    It is part of the firmware.

    0 讨论(0)
提交回复
热议问题