应用程序关闭时,BroadcastReceiver会使应用程序崩溃

时间:2015-11-29 00:28:32

标签: android broadcastreceiver

当应用程序运行时没有问题,但如果我关闭我的应用程序并将日期更改为我指定在应用程序崩溃时提醒的日期 权限:

  <receiver android:name=".AlertReceiver"
              android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

BroadcastReceiver类

public class AlertReceiver extends BroadcastReceiver{
String name = "";
int id = 0;
String releaseDate = "";
// Called when a broadcast is made targeting this class
@Override
public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Toast.makeText(context, "BOOT" , Toast.LENGTH_LONG).show();
        // Set the alarm here.
        Intent alertIntent = new Intent(context, AlertReceiver.class);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        for (Alarm alarm: MyApp.myAlarmsDB.getAllData()){
            alertIntent.putExtra("id", Integer.parseInt(alarm.get_id()));
            alertIntent.putExtra("name", alarm.get_name());
            alertIntent.putExtra("releaseDate", alarm.get_releaseDate());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, Integer.parseInt(alarm.get_id()), alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager.set(AlarmManager.RTC_WAKEUP, Long.valueOf(Integer.parseInt(alarm.get_alertTime())), pendingIntent);
        }

    } else {
        Intent intActivity = new Intent(context, MainActivity.class);
        intActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intActivity);

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

        //delete from database
        MyApp.myAlarmsDB.deleteFromId(String.valueOf(id));
    }

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

}

不幸的是没有打印堆栈跟踪

0 个答案:

没有答案