重新启动该服务,即使应用程序被强制停止,即使在关闭应用程序后,如何继续运行服务后台? [英] Restart the service even if app is force-stopped and Keep running service in background even after closing the app How?

查看:537
本文介绍了重新启动该服务,即使应用程序被强制停止,即使在关闭应用程序后,如何继续运行服务后台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在后台运行的服务。当用户选中复选框,然后服务启动,当它被选中服务停止什么我的应用程序确实是。这是工作完全正常。但问题是,当我从任务管理器关闭应用程序,然后它也停止服务。我想是保持业务正常运行,即使它是接近从任务管理器。然后,只有这样,才能停止该服务是由用户自己通过unckecking框。

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 working perfectly fine. But the problem is when I closed the app from task manager it then also stops the service. 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.

我怎样才能做到这一点?

How can I achieve this?

下面是我的code

我的主要活动

public class SampleServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if(isChecked) {
                Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_LONG).show();
                startService(new Intent(getBaseContext(), MyService.class));
            } else {
                Toast.makeText(getBaseContext(), "Unchecked", Toast.LENGTH_LONG).show();
                stopService(new Intent(getBaseContext(), MyService.class));
            }
        }

    });
}
}

我的服务类

public class MyService extends Service {
Notify n = new Notify();
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    n.initNotification(getBaseContext(), true);
    return START_STICKY;
}

//method to stop service
public void onDestroy() {
    super.onDestroy();
    n.cancelNotification(getBaseContext());
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
}

更新

如何使这项服务,使其运行像Gtalk的服务?

How can we make this service so that it runs like gtalk service?

推荐答案

我不知道这任务管理器你指的是不同的人会采取不同的,所以我就行动立足我的答案,当用户进入设置 - >管理应用程序和 - >力停止的应用程序在Android的方式给了他。

假设您的服务正在运行的进程的一部分,如果用户强制停止的过程中,你是从的永远的重新运行该服务,直到用户的手动启动你。这是尤其是从 3.0合法及以上版本(检查你的)。这也似乎是合乎逻辑,当你认为有一个应用程序,这使服务启动所有的时间,很讨厌在某些方面的用户。因此,当用户订购的应用程序命中(:)力站),它的不应该重新启动服务,继续窃听用户。

例如,试想一下,如果你能会发生什么创建一个刚吃了你的处理器时间由持有唤醒锁的应用程序,你不能杀了他们。这将是可怕的,一个巨大的安全灾难。

所以,您不会可以通过任何手段,直到用户推出的活动之一,重新启动您的服务。

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.

你也不能禁用强制停止按钮AFAIK。你应该采取的观点,没有什么设备上的是你,除了你的应用程序和(在有限的范围内)的资源,而你允许访问控制。

Finnally,甚至Gtalk的应用程序会屈服于你的意志,如果您希望强制停止。当您使用的使用Gtalk的服务,如PUSH的Gmail(手机,其中每增加心不是固件的一部分)的Gtalk或其他应用程序它只会启动。也看看的Andr​​oid C2DM 这里:

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:

<一个href="http://stackoverflow.com/a/11238779/1218762">http://stackoverflow.com/a/11238779/1218762

这篇关于重新启动该服务,即使应用程序被强制停止,即使在关闭应用程序后,如何继续运行服务后台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆