服务停止从设置中清除数据

贾敏

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

Nish8900

与应用程序关联的数据将不再继续清除。为避免这种情况,您需要将您的应用程序签名为系统应用程序。

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

“强制停止”经历了各种含义的反复。它曾经意味着只杀死所有进程和服务,并且清除数据也将与强制停止相同。该平台还有一些较旧的迭代,不如确定何时禁用该按钮好,这可能就是为什么您看到它在2.2中保持启用的原因。

但是在3.2版中,我相信“强制停止”的含义已更改为将应用程序置于一种状态,在该状态下,该应用程序将无法运行,直到用户做了一些明确地启动它的操作(例如从启动器启动,将其选择为输入法等)。进行更改后,“清除数据”继续终止进程并停止其服务,因此该应用程序未处于完全停止状态,因此该按钮保持启用状态。

编辑:工作代码示例:

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);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章