java - Android: Repeat tasks in background. Does my approach is correct? -
i read several threads repeat asynchronous tasks in background, first used way: https://stackoverflow.com/a/6532298 reasons, seems after sometime (several hours), stopped.
so, using way, don't know if way proceed:
broadcastreceiver
public class retrievedatataskbroadcast extends broadcastreceiver { @override public void onreceive(context context, intent intent) { sharedpreferences msharedpreferences = context.getsharedpreferences(my_pref, 0); int delayinms = msharedpreferences.getint("set_delay_refresh", 20)*60*1000; alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); intent = new intent(context, retrievedataservice.class); pendingintent pi = pendingintent.getservice(context, 0, i, 0); am.cancel(pi); if (delayinms > 0) { am.setinexactrepeating(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime() + delayinms, delayinms, pi); } } }
my service class:
public class retrievedataservice extends service implements onrefreshinterface { private context context; private powermanager.wakelock mwakelock; private static final string tag = "refresh_service"; @override public ibinder onbind(intent intent) { return null; } private void handleintent(intent intent) { powermanager pm = (powermanager) getsystemservice(power_service); mwakelock = pm.newwakelock(powermanager.partial_wake_lock, tag); mwakelock.acquire(); //do work callasynchronoustask(); } @override public int onstartcommand(intent intent, int flags, int startid) { handleintent(intent); return start_not_sticky; } public void ondestroy() { super.ondestroy(); mwakelock.release(); } @override public void oncreate() { super.oncreate(); context = this; callasynchronoustask(); } public void callasynchronoustask() { //my asynchronous task => execute class extends asynctask [...] } @override public void onrefreshinterface(int cb_val1, int cb_val2) { //callback when refresh done [...] } }
androidmanifest.xml:
<uses-permission android:name="android.permission.wake_lock"/> <uses-permission android:name="android.permission.receive_boot_completed" /> ... <service android:name="com.example.package.retrievedataservice" android:enabled="true" android:label="refresh data"> </service> <receiver android:name="com.example.package.retrievedatataskbroadcast" android:enabled="true" android:permission="android.permission.receive_boot_completed" > <intent-filter> <action android:name="android.intent.action.boot_completed" /> <action android:name="android.intent.action.quickboot_poweron" /> </intent-filter> </receiver>
i added way launch service when app starts: mainactivity:
@override protected void onresume() { super.onresume(); thread t = new thread(){ public void run(){ sharedpreferences msharedpreferences = getsharedpreferences(pref, 0); int delayinms = msharedpreferences.getint("set_delay_refresh", 20)*60*1000; alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); intent = new intent(mainbaseactivity.this, retrievedataservice.class); pendingintent pi = pendingintent.getservice(mainbaseactivity.this, 0, i, 0); am.cancel(pi); am.setinexactrepeating(alarmmanager.elapsed_realtime_wakeup, systemclock.elapsedrealtime() + delayinms, delayinms, pi); } }; t.start(); }
thank , advice.
you dont need service launch repeated task when device starts. task never run when device off.
you can set repeating alarm using alarm manager. if trigger time specify in past when device off, alarm triggers when device turns on.
check - https://developer.android.com/training/scheduling/alarms.html
Comments
Post a Comment