[英]Broadcast receiver not firing
我正在編寫一個應用程序,一旦到達特定的日期/時間,它將觸發特定的方法。 但是,我的廣播接收器未觸發...我可以看到鬧鍾已設置。
我在活動中的方法:
private void setAlarm(Date date){
Intent activate = new Intent(this, Alarm.class);
AlarmManager alarms ;
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(date.getTime());
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Log.d("ALARM","Setting alarm");
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
Log.d("ALARM","Setting time "+cal.getTimeInMillis());
}
我的廣播接收者:
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("ALARM", "RING RING");
}
}
我在<application>
標記內的清單:
<receiver android:name=".Alarm" android:process=":remote" >
<intent-filter>
<action android:name="com.package.feature.subpackage.arrangealarm.ALARM_ACTION"/>
</intent-filter>
</receiver>
有誰知道為什么不觸發?
您的代碼似乎沒有問題(我復制了代碼段,並讓Android Studio像我一樣為它設置了android:process=":remote"
生成BroadcastReceiver)。
當我運行該應用程序時,會觸發BroadcastReceiver
但只有在Logcat設置(在右側)中選擇“無過濾器”時,才可以看到相應的Logcat條目,而當我選擇“僅顯示所選應用程序”時,則只能看到相應的Logcat條目。
發生這種情況是因為您使用了android:process=":remote"
。 BroadcastReceiver將在“主”應用程序進程之外的其他進程中運行,因此“ RING RING”將出現在另一個進程ID下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.