重新启动后缺少意图的“ extras”值

时间:2018-06-26 13:17:42

标签: java android notifications alarmmanager android-pendingintent

我刚刚发现,我不知道如何在设备重启后保存我的Intent附加功能。我需要它来进行临时通知。

这是我设置闹钟的 MainActivity 的一部分

Calendar startTime = Calendar.getInstance();
            startTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            startTime.set(Calendar.MINUTE, minute);
            startTime.set(Calendar.SECOND, 0);
            long alarmStartTime = startTime.getTimeInMillis();

            Intent intent = new Intent(context, AlarmReceiver.class);
            intent.putExtra("notificationId", notificationId);
            intent.putExtra("text", text);
            intent.putExtra("alarmStartTime", alarmStartTime);
            intent.setAction("android.intent.action.BOOT_COMPLETED");
            intent.setAction("android.intent.action.QUICKBOOT_POWERON");

            PendingIntent alarmIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);


            alarm.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime, AlarmManager.INTERVAL_DAY, alarmIntent);

AlarmReceiver

public void onReceive(Context context, Intent intent) {

    Bundle extras = intent.getExtras();
    if (extras != null){
        int notificationId = extras.getInt("notificationId");
        String message = extras.getString("text");

        String action = intent.getAction();
        if (BOOT_COMPLETED.equals(action) ||
                QUICKBOOT_POWERON.equals(action)) {

            Intent service = new Intent(context, BootService.class);
            service.setAction(message);
            service.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            service.putExtra("notificationId", notificationId);
            service.putExtra("text", message);
            context.startService(service);
        }
    }
}

BootService 中的方法,我需要在其中获取notificationId和消息

private void setAlarm(int Id, String message) {
    Context context = MyApp.getContext();
    Intent mainIntent = new Intent(context, MainActivity.class);
    mainIntent.setAction("android.intent.action.BOOT_COMPLETED");
    mainIntent.setAction("android.intent.action.QUICKBOOT_POWERON");

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager myNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    builder.setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Wake up! " + Id)
            .setContentText(message)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent);
    myNotificationManager.notify(Id, builder.build());
}

@Override
protected void onHandleIntent(Intent intent) {

    int Id = intent.getExtras().getInt("notificationId", 0);
    String message = intent.getExtras().getString("text");
    setAlarm(Id, message);
    Context context = MyApp.getContext();
    Intent service = new Intent(context, BootService.class);
    stopService(service);
}

一切正常,但重新启动后,我的其他设备为空。谢谢。

1 个答案:

答案 0 :(得分:0)

如果您需要永久保存一些数据,则可以创建例如包含所有附加值的 HasMap

root.findall(".//hostscript/..[@name='host']")

每次更新一些值时,您都可以序列化此Map并将其保存在一个文件中,该文件即使在重启后也始终可用

序列化并保存

Map<String,String> extras = new HasMap<Strimg,String>();
extras.put("notificationId", notificationId);
extras.put("text",text);
.
.

最后,在重新启动后首次运行应用程序时,您将在Map Extras中关联存储在序列化文件中的对象

还原Hasmap

FileOutputStream fos =new FileOutputStream("hashmap.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(extras);
oos.close();
fos.close();