【问题标题】:App crashes when alarms get received收到警报时应用程序崩溃
【发布时间】:2015-11-18 02:10:58
【问题描述】:

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() 中运行,如果没有,它会打开应用程序?

【问题讨论】:

  • 请发布堆栈跟踪/日志。
  • 请发布堆栈跟踪和createNotification() 方法。
  • 创建通知不是问题,相信我
  • 没有打印堆栈跟踪/日志抱歉\:(
  • 如果没有堆栈跟踪和/或更多代码,我们将无法帮助您。

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


【解决方案1】:

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

getStringExtra("something");

正在返回a null string, check documentation here

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

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多