收到警报后应用程序崩溃

时间:2015-11-18 02:10:58

标签: android android-intent broadcastreceiver android-pendingintent android-broadcast

MobMy BrodcastReceiver的OnReceive,它只在应用程序在后台运行时才能正常工作,我收到通知没有问题,但是一旦用户关闭应用程序并等待他指定提醒它崩溃的日期,它因为应用程序没有运行?所以我猜它是因为我的应用程序没有运行所以无法检索意图?这就是崩溃的原因?

我如何设置闹钟,非常基本的东西;

   public void setAlarm(View view) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, yearDate);
    cal.set(Calendar.MONTH, monthDate);
    cal.set(Calendar.DAY_OF_MONTH, dayDate);
    long alertTime = cal.getTimeInMillis();

    Intent alertIntent = new Intent(this, AlertReceiver.class);
    // store id
    alertIntent.putExtra("id", mainId);
    alertIntent.putExtra("name", name);
    alertIntent.putExtra("releaseDate", releaseDate);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, mainId, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);

}

我的BrodcastReceiver的OnReceive

  public void onReceive(Context context, Intent intent) {

    int id = intent.getIntExtra("id", -1);
    String name = intent.getStringExtra("name");
    String releaseDate = intent.getStringExtra("releaseDate");

    createNotification(context, "Movie Reminder", name + ": " + releaseDate, "Movie Reminder", id);
}

也许我可以检查我的应用是否未在onReceive()中运行,如果没有,它会打开应用程序?

1 个答案:

答案 0 :(得分:0)

有许多问题可能是错误的,没有Logcat,我们无法确定,但这里有一些最可能出现的问题:

getStringExtra("something");

正在返回a null string, check documentation here

上下文没有createNotification方法,要么创建一个静态方法并使用您自己的方法,要么check here how to create a notificationhere for a step-by-step guide

相关问题