HelloCW
HelloCW

Reputation: 2235

How to design a task recurrently every 10 minutes in Android?

I hope to do a restore operation recurrently every 10 minutes, and I hope that the function will be keep live even if I restart my mobile phone.

The following content if my planning, I don't know whether it's right, or do you have more better way?

Step 1: Invoke EnableCleanupService(Context mContext) in a Activity, the system will do a restore operation recurrently every 10 minutes, the system will keep to do a restore operation recurrently every 10 minutes even if I close the APP, right?

Step 2: In order to do the restore operation recurrently every 10 minutes after I restart my mobile phone, the system invoke EnableCleanupService(context) on onReceive(Context context, Intent intent) automatically even if I don't open the App. Right?

Step 1

public static void EnableCleanupService(Context mContext){
        AlarmManager alarmManager;
        alarmManager = (AlarmManager)mContext.getSystemService(mContext.ALARM_SERVICE);

        long now = System.currentTimeMillis();       
        now= now+mContext.getResources().getInteger(R.integer.FirstTigger)*60*1000;
        int nextTime=mContext.getResources().getInteger(R.integer.ScheduleTimeMin)*60*10*1000;
        alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                now,
                nextTime,
                GetPendingIntent(mContext)
        );
    }


private static PendingIntent GetPendingIntent(Context mContext){
        PendingIntent pendingIntent=null;

        pendingIntent = PendingIntent.getService(mContext,
                0,
                new Intent(mContext, CleanupService.class),
                PendingIntent.FLAG_CANCEL_CURRENT);

        return pendingIntent;
}


public class CleanupService  extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {              
        //Do Task
    }
}

Step 2

<receiver android:name="bll.CleanupBootReceiver">
   <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</receiver>


public class CleanupBootReceiver  extends BroadcastReceiver{
    private static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {        
        if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
            EnableCleanupService(context)
        }
    }

}

Upvotes: 0

Views: 103

Answers (1)

Irshad Kumail
Irshad Kumail

Reputation: 1273

What you have done seems to be correct.In my opinion AlarmManager is the best if you want to perform any time-based or time-bound operation. If you have any doubts regarding AlarmManager. You can read this Working with AlarmManager | With Example

One important thing though. Setting a repetitive Alarm every 10 minutes is a very bad experience, it drains the battery and also some custom cleaner applications might cancel your Alarm as "spam".

Upvotes: 1

Related Questions