For the scheduling part you can use the AlarmManager
For instance:
public class TaskScheduler {
public static void startScheduling(Context context) {
Intent intent = new Intent(context, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 600, pendingIntent);
}
}
Then inside your receiver class you can start an IntentService:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intentService = new Intent(context, MyService.class);
context.startService(intentService);
}
}
MyService
looks roughly like:
class MyService extends IntentService {
public MyService() {
super(MyService.class.getSimpleName());
}
@Override
public void onHandleIntent(Intent intent) {
// your code goes here
}
}
And finally, don't forget to register MyReceiver
in the manifest file:
<receiver
android:name="Your.Package.MyReceiver">
</receiver>
As well as your service:
<service
android:name="...">
</service>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…