java - 使用 AlarmManager 安排时 Android 通知未发布

标签 java android android-intent android-notifications

我尝试在一定时间间隔后从我的游戏中发布通知。我从 BroadcastReciever 类的 onReceive() 方法中调用 PostNotification() 函数,以在游戏开始 1 分钟后发布通知。

广播接收器

public class NotificationReciever extends BroadcastReceiver
{

private static int count=0;

@Override
public void onReceive(Context context, Intent intent)
{
    try 
    {
         Bundle bundle = intent.getExtras();
         String message = bundle.getString("message");
         int id = bundle.getInt("id");
         PostNotification(message, id);
    }
    catch (Exception e) 
    {
         e.printStackTrace();
    }
    wl.release();
}

public void PostNotification(String notif, int id)
{
    Notification notify=new Notification(R.drawable.icon,
            notif,
            System.currentTimeMillis());
Intent intent = new Intent(MyUtil.getInstance().context, MyActivity.class);
        PendingIntent i=PendingIntent.getActivity(MyUtil.getInstance().context, 0, intent, 0);
        notify.setLatestEventInfo(MyUtil.getInstance().context, "Title", notif, i);  
        MyActivity.notifyMgr.notify(id, notify);
        }
    }
}

我正在从 MyActivity 的 onCreate() 调用 ScheduleNotification()

public void ScheduleNotification()
{
     Calendar cal = Calendar.getInstance();
     Intent intent = new Intent(MyUtil.getInstance().context, NotificationReciever.class);
     intent.putExtra("id", NOTIFY_ID);
     intent.putExtra("message", "message");
     PendingIntent sender = PendingIntent.getBroadcast(MyUtil.getInstance().context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}

但是我没有收到任何通知,并在我的 logcat 中收到以下错误

  01-11 19:28:32.455: W/System.err(20661): java.lang.NullPointerException
01-11 19:28:32.475: W/System.err(20661):    at android.content.ComponentName.<init>(ComponentName.java:75)
01-11 19:28:32.475: W/System.err(20661):    at android.content.Intent.<init>(Intent.java:2893)
01-11 19:28:32.475: W/System.err(20661):    at com.games.TestGame.NotificationReciever.PostNotification(NotificationReciever.java:41)
01-11 19:28:32.475: W/System.err(20661):    at com.games.TestGame.NotificationReciever.onReceive(NotificationReciever.java:27)

我知道在创建通知 Intent 时我做错了。当我直接从我的 Activity 中调用时,我会正确收到通知,但当我通过警报调用它时,会出现问题

Intent intent = new Intent(MyUtil.getInstance().context, MyActivity.class);

谁能告诉我哪里出错了。

最佳答案

您使用的通知构造函数的when参数仅用于显示目的。它不会延迟通知的显示。

使用闹钟怎么样?但它只接受 Intent。

关于java - 使用 AlarmManager 安排时 Android 通知未发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14276050/

相关文章:

java - 将 vector 添加到现有的 jTable 以显示数据库中的记录

java - gson 和引用资料

android - 在 Activity 之外的 Webview 中显示 AlertDialog

android - 模拟器无法完全启动,工作了几个月突然停止了。怀疑 GL 库或硬件

android - 如何从 Intent 中获取文件名?

java - 如何让我的搜索栏覆盖工具栏中的标题?

java - Tomcat:实现 java.security.Principal

java - import com.google 无法解决?

android - 按下后退按钮调用 onCreate (Android)

java - 尝试打开文件时,不同 Android 版本的 URI 方案不同