MainActivity中的Android类未从警报管理器接收广播

Zehata

我正在尝试使用警报管理器发送和接收广播,该广播将调用创建通知的方法。我希望即使关闭应用程序也会创建通知。我使用此方法创建了一个类,该类在“警报管理器”中创建警报:

@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。同样,它也不是由于构建错误或运行时异常引起的。

迈克·M

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

无论选择哪种方式,都需要在Receiver类中进行一些更改。你需要调用getSystemService()Context传递到onReceive()方法,因为它以前被称为在当前MainActivity情况下,这将不再有机会获得。另外,您需要限定NOTIFICATION_SERVICE常量。

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

最后,因为要广播到接收方,而不是启动,所以PendingIntent广播的警报必须是广播更改PendingIntentServicegetService()getBroadcast()

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章