Reputation: 11
I am using an intent service. Whenever I clear my cache(by clicking recent tasks button and then clicking clear cache icon) my service closes(if started), even the pending intent i set using alarm manager does not start the service if it is scheduled to start later.
Here's my basic code:
public class MainActivity extends Activity {
SampleAlarmReceiver alarm = new SampleAlarmReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarm.setAlarm(this);
}
}
public class SampleAlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, SampleSchedulingService.class);
startWakefulService(context, service);
}
public void setAlarm(Context context) {
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, SampleAlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void cancelAlarm(Context context) {
if (alarmMgr!= null) {
alarmMgr.cancel(alarmIntent);
}
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
public class SampleBootReceiver extends BroadcastReceiver {
SampleAlarmReceiver alarm = new SampleAlarmReceiver();
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.setAlarm(context);
}
}
}
public class SampleSchedulingService extends IntentService {
public SampleSchedulingService() {
super("SchedulingService");
}
public static final String TAG = "Scheduling Demo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
@Override
protected void onHandleIntent(Intent intent) {
//my work
SampleAlarmReceiver.completeWakefulIntent(intent);
}
}
Most part was taken from sample code in: https://developer.android.com/training/scheduling/alarms.html
What do I do to keep the service running or restart after clearing cache. Also, how to make sure pending intent will start the service even after clearing the cache.
Thanks Help is appreciated:)
Upvotes: 1
Views: 1631
Reputation: 2375
This is the expected behavior upon clearing the cache or data of the Android application.
When the application data is cleared, the Android OS shutsdown the application(if running) and clears all the services and broadcast receivers associated with the app. Even Alarm Managers are cleared.
Why does this happen? You see, the services and receivers, utilize the app data for performing several operations. Therefore, if your services weren't shut down on clearing data, it might raise several exceptions while running since the data on which it operates simply doesn't exist anymore!
There is currently no solution for this. What you can do is you can restart your services and receivers on the app launch.
Please also mention in comments if you are clearing the app cache or data? You can refer this question for more info.
Upvotes: 1