K
K
korvin22018-06-28 11:44:08
Android
korvin2, 2018-06-28 11:44:08

How to save and Retrieve(!) Intent extra's data after reload?

Hello
Such a thing: you need the ability to create several notifications that will be repeated every day at the time specified. In other words, scheduled notifications.
Everything works well, but only after the reboot, only the first notification is shown (and then, its id and text are lost), and all notifications that are "recorded" after it disappear. The matter is that BroadcastReceiver loses the data which I send through Intent.
Part of the code from MainActivity , which is activated when the button is clicked. This is where I set the notificationId and text data to the Intent :

spoiler
int notificationId = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
                String text = etText.getText().toString();
                Context context = MyApp.getContext();
                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_UPDATE_CURRENT);
                AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime, AlarmManager.INTERVAL_DAY, alarmIntent);
AlarmReceiver that receives this data
spoiler
private static final String BOOT_COMPLETED =
            "android.intent.action.BOOT_COMPLETED";
    private static final String QUICKBOOT_POWERON =
            "android.intent.action.QUICKBOOT_POWERON";
    public static Context appContext;

    @Override
    public void onReceive(Context context, Intent intent) {

        appContext = MyApp.getContext();

        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 , which receives data already from the AlarmReceiver and creates a notification
spoiler
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, Id, 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);
        service.setAction(message);
        service.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        stopService(service);
    }
AndroidManifest
spoiler
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a52780.nontifications"
    android:installLocation="internalOnly">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".MyApp">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.example.a52780.nontifications.AlarmReceiver"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.example.a52780.nontifications.BootService"
            android:enabled="true"
            android:exported="false"/>


    </application>

</manifest>

Actually, that's all. I understand that Intents are extras and should not persist across reloads. But it just does not occur to me how else to save the id and text of the notification. There was an idea to use SharedPreferences, but only there each notification will still have to set a unique key. And how then can I get these keys without knowing them? Such is the vicious circle. Most likely I made some stupid mistake or I don't know something important, but I'm new to Android, so I hope for your help :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2018-06-28
@402d

https://github.com/AnkitSinhal/JobSchedulerExample

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question