MainActivity中的Android类不从Alarm Manager接收广播

时间:2016-08-20 05:47:32

标签: android broadcastreceiver alarmmanager

我正在尝试使用Alarm Manager发送和接收将调用创建通知的方法的广播。我希望即使应用关闭也会创建通知。我使用此方法创建了一个类,它在Alarm Manager中创建警报:

@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);            
}

这是接收器,我把它放在主要活动中:

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>
...

我完成了一个Alarm Manager转储并在里面找到了广播。但是,当警报管理器触发时,无论应用程序处于后台还是应用程序中,都不会调用loadqotd。此外,它既不是由于构建错误或运行时异常。

1 个答案:

答案 0 :(得分:1)

当您使用嵌套类作为应用程序组件时 - 就像问题中的loadqotd类一样 - 它必须是public static。或者,您可以将其移动到单独的类文件中,并相应地更新清单中的name

无论您选择哪种方式,都需要在Receiver类中更改一些内容。您需要在传递到getSystemService()方法的Context上调用onReceive(),因为它之前已在当前MainActivity实例上调用,它将不再具有进入。此外,您还需要限定NOTIFICATION_SERVICE常量。

NotificationManager notificationManager =
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

最后,警报的PendingIntent需要是广播PendingIntent,因为您正在向接收方广播,而不是启动Service。将getService()更改为getBroadcast()

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarmintent, 0);