服务停止从设置中清除数据 [英] Service stops on clearing data from settings

查看:169
本文介绍了服务停止从设置中清除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我运行服务,其中使用 broadcastReciever 记录文本文件上的传入和传出呼叫记录(保存在内部存储中).但是当我按下清除数据按钮(设置=>应用程序=> Myapp =>清除数据)时,我的服务也停止了.我在onDestroy()方法中使用了Log.d(),但在按clear data时未登录到logcat中. 我阅读了此问题,它具有相同的问题,但是我没有找到任何解决方案.然后我通过了开发人员指南.我真的很困惑.

in my app i run a service in which i keep record of incoming and out going calls on text file(saved it on internal storage) using broadcastReciever. but when i press clear data button(settings=>apps=>Myapp=>clear data) my service also stops. i used Log.d() in onDestroy() method but it is not logged in logcat when i press clear data. i read this question having same problem but i didn't find any solution. then i went through Developer Guide. i am really confused.

推荐答案

与应用程序关联的数据在清除时将不再保留.为避免这种情况,您需要将您的应用程序签名为系统应用程序.

清除数据确实会杀死该应用程序,并且一直存在.

Clear Data does kill the app, and always has.

"Force Stop"经历了各种含义的反复.它用了 意味着杀死所有进程和服务,并清除数据 也将与强制停止相同.也有比较老的 该平台的迭代效果不如确定何时 禁用按钮,这可能是您看到它仍然存在的原因 在2.2中启用.

"Force Stop" has gone through various iterations of meanings. It used to mean to just kill all processes and services, and clearing data would also do the same as a force stop. There were also older iterations of the platform that were not as good as figuring out when to disable the button, which is probably why you are seeing it remain enabled in 2.2.

但是在3.2中,我相信将强制停止"的含义更改为 应用程序处于无法运行的状态,直到 用户做了一些明确地启动它的操作(例如启动它) 从启动器中选择它作为输入法,等等).当那改变 做出来之后,清除数据"继续杀死进程并停止 它的服务,因此该应用未处于完全停止状态,因此 按钮保持启用状态.

However in 3.2 I believe the meaning of "Force Stop" change to put the application in a state where it would not be able to run until the user had done something to explicitly start it (such as launching it from launcher, selecting it as an input method, etc). When that change was made, "Clear Data" continued to just kill the processes and stop its services, so the app was not in the full stopped state so the button remains enabled.

工作代码示例:

Step 1) Create one Android BroadCastReciever and register it for two action

Manifest

    <service android:name="ServiceTest" >
    </service>

    <receiver android:name="ReceiverCall" >
        <intent-filter>
            <action android:name="com.android.techtrainner" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
com.android.techtrainner is the custom action. BroadCastReceiver Class contain code to restart service again

public class ReceiverCall extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Service Stops", "Ohhhhhhh");
        context.startService(new Intent(context, ServiceTest.class));;
    }

}
Step 2) Create Android service class , do some task there (I have taken one Timer to print Log) and in onDestory()

public void onDestroy() {
    try {
        mTimer.cancel();
        timerTask.cancel();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Intent intent = new Intent("com.android.techtrainner");
    intent.putExtra("yourvalue", "torestore");
    sendBroadcast(intent);
}

这篇关于服务停止从设置中清除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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