android - BroadcastReceiver is not called by alarm -
there lot of related questions , beleive i've read them twice or so. reason seem have tomatoes on eyes. not see error.
i've got alarm set using alarm manager. (happy share more code if required.)
private static string alarm_action = "de.klecker.bigben.alarm"; private static pendingintent creatependingintent() { intent alarmintent = new intent(getcontext(), bigbenalarm.class); alarmintent.setaction(alarm_action); return pendingintent.getbroadcast(getcontext(), 0, alarmintent, 0); }
getcontext()
returns reference application.
public static void setalarmfromnow() { alarmmanager manager = (alarmmanager) getcontext().getsystemservice(context.alarm_service); pendingintent intent = creatependingintent(); // first cancel ongoing alarm manager.cancel(intent); manager.set(alarmmanager.rtc_wakeup, system.currenttimemillis() + (5 * 1000), intent); log.d("bigben", "alarm set!"); }
the alarm registered. extract of output of
adb shell dumpsys alarm
batch{2f6ac936 num=1 start=68344020 end=68344020}: rtc_wakeup #0: alarm{f8a5037 type 0 when 1453976393037 de.klecker.bigben} tag=*walarm*:de.klecker.bigben.alarm type=0 whenelapsed=+4s155ms when=2016-01-28 11:19:53 window=-1 repeatinterval=0 count=0 operation=pendingintent{3df19a4: pendingintentrecord{18a6130d de.klecker.bigben broadcastintent}}
and got fired. extract log:
01-28 11:19:53.038 v/alarmmanager( 885): sending alarm {18a6130d type 0 *walarm*:de.klecker.bigben.alarm} 01-28 11:19:53.039 v/alarmmanager( 885): done {18a6130d, *walarm*:de.klecker.bigben.alarm} [1ms]
but receiver did not called. receiver class:
public class bigbenalarm extends broadcastreceiver { @override public void onreceive(context context, intent intent) { vibrator vibrator = (vibrator) context.getsystemservice(context.vibrator_service); vibrator.vibrate(2000); log.d("bigben", "alarm fired!!!"); } }
neither log statement appears on log, nor phone vibrate. (virbration permission declared in manifest). tried toasts , playing sounds , setting breakpoint, apparently, method never called.
this how receiver registered:
androidmanifest.xml: <receiver android:name="de.klecker.bigben.bigbenalarm"> <intent-filter> <action android:name="de.klecker.bigben.alarm"/> </intent-filter> </receiver>
so important detail did miss?
thanks tried hard. , yes, did have tomatoes on eyes. although quite embarassing leave in , provide answer in case else runs same issue.
the big mistake within android manifest. receivers need registered within application tags.
androidmanifest.xml: <application android:name=".bigbenapp" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="big ben" android:supportsrtl="true" android:theme="@style/apptheme" > <activity android:name=".bigbenactivity"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <receiver android:name="de.klecker.bigben.bigbenalarm" android:enabled="true" > <intent-filter> <action android:name="de.klecker.bigben.alarm"/> </intent-filter> </receiver> </application>
for reason closed application , tried register receiver.
Comments
Post a Comment