1

抱歉愚蠢的问题...我尝试在 AlarmManager 类上集成通知栏(相同的代码在新活动中完美运行)。

Context context = getApplicationContext(); and
Intent notificationIntent = new Intent(this, AlarmManager.class);

这不正确吗?

    public class AlarmReceiver extends BroadcastReceiver {

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();



        mNotificationManager = (NotificationManager) context.getSystemService(ns);
        int icon = android.R.drawable.stat_notify_chat;
        CharSequence tickerText = "tickerText";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);

        Context context = getApplicationContext();
        CharSequence contentTitle = "Title";
        CharSequence contentText = "Text";
        Intent notificationIntent = new Intent(this, AlarmManager.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

mNotificationManager.notify(NOTIFICATION_ID, notification);
4

1 回答 1

1

您需要调用context.getSystemService(ns)where context 是传递给 onCreate 的变量。

于 2012-12-14T17:25:51.350 回答