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.
问题:
回答1:
The data associated the the app will no longer persist on clearing. To avoid this you need to sign your app as a system app.
Clear Data does kill the app, and always has.
"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.
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.
Edit: Example of working code:
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);
}