Android Class 在 MainActivity 中没有收到来自警报管理器的广播
Android Class in MainActivity does not receive Broadcast from Alarm Manager
我正在尝试使用警报管理器来发送和接收将调用方法创建通知的广播。我希望即使在应用程序关闭时也能创建通知。我用这种方法创建了一个 class,它在警报管理器中创建了警报:
@android.webkit.JavascriptInterface
public void qotd(String toast, int hour, int minute) {
// for testing purposes
Toast.makeText(mContext, (String.valueOf(hour) + " " + String.valueOf(minute)), Toast.LENGTH_SHORT).show();
// cancel other alarms first
Intent alarmtocancel = new Intent(mContext, loadqotd.class);
PendingIntent cancelIntent = PendingIntent.getBroadcast(mContext, 1001, alarmtocancel, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(mContext.ALARM_SERVICE);
am.cancel(cancelIntent);
cancelIntent.cancel();
// create new alarm
Intent alarmintent = new Intent(mContext, loadqotd.class);
AlarmManager setam = (AlarmManager) getSystemService(mContext.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, alarmintent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
setam.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}
这是接收器,我把它放在 Main 里面 Activity:
public class loadqotd extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// testing purposes
Toast.makeText(context, "received", Toast.LENGTH_SHORT).show();
Intent resultIntent = new Intent(context, QuestionOfTheDay.class);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Your Question of the Day is ready!")
.setContentText("Check out the question of today")
.setContentIntent(PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT));
int notid = 001;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notid, mBuilder.build());
}
}
这是清单:
...
<application>
<receiver android:name=".MainActivity$loadqotd"></receiver>
</application>
...
我做了一个报警管理器转储并在里面找到了广播。但是,当警报管理器触发时,永远不会调用 loadqotd,无论是在应用程序处于后台还是在应用程序中时。此外,它既不是由于构建错误也不是运行时异常。
当您使用嵌套 class 作为应用程序组件时 - 如问题中的 loadqotd
class - 它必须是 public static
。或者,您可以将其移动到单独的 class 文件,并相应地更新清单中的 name
。
无论选择哪种方式,都需要在 Receiver 中进行一些更改 class。您需要在传递给 onReceive()
方法的 Context
上调用 getSystemService()
,因为它之前是在当前 MainActivity
实例上调用的,它将不再具有进入。此外,您需要限定 NOTIFICATION_SERVICE
常量。
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
最后,您的闹钟 PendingIntent
需要广播 PendingIntent
,因为您正在向接收器广播,而不是启动 Service
。将 getService()
更改为 getBroadcast()
。
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarmintent, 0);
我正在尝试使用警报管理器来发送和接收将调用方法创建通知的广播。我希望即使在应用程序关闭时也能创建通知。我用这种方法创建了一个 class,它在警报管理器中创建了警报:
@android.webkit.JavascriptInterface
public void qotd(String toast, int hour, int minute) {
// for testing purposes
Toast.makeText(mContext, (String.valueOf(hour) + " " + String.valueOf(minute)), Toast.LENGTH_SHORT).show();
// cancel other alarms first
Intent alarmtocancel = new Intent(mContext, loadqotd.class);
PendingIntent cancelIntent = PendingIntent.getBroadcast(mContext, 1001, alarmtocancel, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(mContext.ALARM_SERVICE);
am.cancel(cancelIntent);
cancelIntent.cancel();
// create new alarm
Intent alarmintent = new Intent(mContext, loadqotd.class);
AlarmManager setam = (AlarmManager) getSystemService(mContext.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, alarmintent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
setam.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}
这是接收器,我把它放在 Main 里面 Activity:
public class loadqotd extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// testing purposes
Toast.makeText(context, "received", Toast.LENGTH_SHORT).show();
Intent resultIntent = new Intent(context, QuestionOfTheDay.class);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Your Question of the Day is ready!")
.setContentText("Check out the question of today")
.setContentIntent(PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT));
int notid = 001;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notid, mBuilder.build());
}
}
这是清单:
...
<application>
<receiver android:name=".MainActivity$loadqotd"></receiver>
</application>
...
我做了一个报警管理器转储并在里面找到了广播。但是,当警报管理器触发时,永远不会调用 loadqotd,无论是在应用程序处于后台还是在应用程序中时。此外,它既不是由于构建错误也不是运行时异常。
当您使用嵌套 class 作为应用程序组件时 - 如问题中的 loadqotd
class - 它必须是 public static
。或者,您可以将其移动到单独的 class 文件,并相应地更新清单中的 name
。
无论选择哪种方式,都需要在 Receiver 中进行一些更改 class。您需要在传递给 onReceive()
方法的 Context
上调用 getSystemService()
,因为它之前是在当前 MainActivity
实例上调用的,它将不再具有进入。此外,您需要限定 NOTIFICATION_SERVICE
常量。
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
最后,您的闹钟 PendingIntent
需要广播 PendingIntent
,因为您正在向接收器广播,而不是启动 Service
。将 getService()
更改为 getBroadcast()
。
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarmintent, 0);