android - How do I configure my AlarmReceiver to actually trigger at my desired interval? -
the goal portion of app run repeating alarm in background @ times grabs new prediction every 15 minutes server side machine learning algorithm, updating app.
i have skeleton desired behavior implemented, make sure methodology correct. skeleton supposed trigger toast every 10 seconds stating alarm working. however, after set alarm, never see message. have included write console, never appears well, leading me believe not entirely understand how alarm receiver works.
here main activity class instantiates alarm , receiver:
public class mainactivity extends appcompatactivity implements timepickerfragment.fragmentcallbacks { private pendingintent pendingintent; private alarmmanager manager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // retrieve pendingintent perform broadcast intent alarmintent = new intent(this, predictionupdatereceiver.class); pendingintent = pendingintent.getbroadcast(this, 0, alarmintent, 0); startalarm(); //... } //... public void startalarm() { manager = (alarmmanager) getsystemservice(context.alarm_service); int interval = 10000; manager.setrepeating(alarmmanager.rtc_wakeup, system.currenttimemillis(), interval, pendingintent); toast.maketext(this, "alarm set", toast.length_short).show(); } }
here alarm receiver class:
public class predictionupdatereceiver extends broadcastreceiver { @override public void onreceive(context arg0, intent arg1) { // our recurring task, we'll display message toast.maketext(arg0, "i'm running", toast.length_short).show(); system.out.print("alarm activated"); } }
and have updated manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.habitabilitystudy" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="11" android:targetsdkversion="22" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <receiver android:name=".predictionupdatereceiver"></receiver> <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
well, have couple of issues.
first, setrepeating()
inexact on android 4.4 , higher, targetsdkversion
of 19 or higher. not state how "inexact" is, , don't know when alarms scheduled.
second, a repeating alarm period of less 1 minute not allowed on android 5.1+, due undocumented regression. periods under minute rounded 1 minute. so, not control in 10 seconds regardless of inexactness.
finally, changes coming in next version of android means not going control periodically long while device idle (and not charging) or if user not return app long time (while device not charging). that's not affecting right now, may wish ponder whether approach whatever doing work going forward.
you can use adb shell dumpsys alarm
see if alarm scheduled. unfortunately, unless fixed things, not tell when alarm run, due inexactness issue.
also, recommend using log.d()
, or @ worst system.out.println()
, instead of system.out.print()
.
Comments
Post a Comment