在我的应用程序中运行服务,其中我使用 broadcastReciever 在文本文件上保存传入和传出呼叫的记录(保存在内部存储上)。但是当我按下清除数据按钮(settings => apps => Myapp =>清除数据)时,我的服务也会停止。我在Log.d()
方法中使用了onDestroy()
但是当我按clear data
时它没有记录在logcat中。
我读this question有同样的问题,但我没有找到任何解决方案。然后我经历了Developer Guide。我真的很困惑。
答案 0 :(得分:3)
与应用相关联的数据将不再保留清除。为避免这种情况,您需要将应用程序签名为系统应用程序。
清除数据确实会杀死该应用,并且始终具有。
"强制停止"经历了各种意义的迭代。它用了 意味着只是杀死所有进程和服务,并清除数据 也会像强制停止那样做。还有更老的 平台的迭代不如找出什么时候那么好 禁用按钮,这可能是你看到它仍然存在的原因 在2.2中启用。
然而在3.2中我相信"强制停止"改变把 应用程序处于无法运行的状态 用户已经做了一些事情来明确启动它(例如启动它 从启动器,选择它作为输入方法,等)。当那个改变 制作了,#34; Clear Data"继续只是杀死进程并停止 它的服务,所以应用程序没有处于完全停止状态所以 按钮保持启用状态。
修改强> 工作代码示例:
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);
}