Skip to content Skip to sidebar Skip to footer

Service Stops On Clearing Data From Settings

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 but

Solution 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:

Step1) Create one AndroidBroadCastReciever and register it for two action

Manifest

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

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

publicclassReceiverCallextendsBroadcastReceiver {

    @OverridepublicvoidonReceive(Context context, Intent intent) {
        Log.i("Service Stops", "Ohhhhhhh");
        context.startService(newIntent(context, ServiceTest.class));;
    }

}
Step2) CreateAndroid service class , do some task there (I have taken one Timer to print Log) and inonDestory()

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

Post a Comment for "Service Stops On Clearing Data From Settings"