android services life time continuation

2019-07-21 08:25发布

问题:

In mainactivity I have Broadcast Receiver, pending intent, and alarm manager. It triggers as per selected time (System.currentTimeMillis() + smstimeinmilliseconds).

Intent intent = new Intent(this, DBBroadcastReceiver.class);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + smstimeinmilliseconds, pendingIntent);

On selected time, this pending intent triggers broadcast receiver.

public class DBBroadcastReceiver extends BroadcastReceiver 

@Override
public void onReceive(Context context, Intent intent)
{
message = intent.getStringExtra("message");

}

I can set message in activity and set time in alarm manager. Every thing works flawless. I can activate and deactivate this. But if i set few alarm mangers in future time and reboot my mobile. all alarm manager destroy .....

Kindly tell me in steps and sequence what to do with activity , broadcast receiver and do i need service , if yes then how can i use it.

回答1:

You need a BoradcastReceiver to be called on boot up.

Then you need in your manifest :

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

<receiver
    android:name=".broadcasts.YourBroadcastReceiverName">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

And this broadcast receiver needs to schedule again all the alarms. Something like :

public class YourBroadcastReceiverName extends BroadcastReceiver {

    private AlarmManagerFacade alarmManager;

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

        // Retreive data related to alarms
        Cursor cursor = context.getContentResolver().query(Alarm.CONTENT_URI, null,
                Alarm.COLUMN_ACTIVE + " = ? ",
                new String[] { String.valueOf(1) }, "");

        if (cursor.moveToFirst()) {

            // Schedule all the active alarms.
            alarmManager = new AlarmManagerFacade(context);
            do {
                // TODO : Schedule alarm according to data in cursor.
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
}

(This code is coming from one of my app, some of the objects are not available in the Android SDK)

In order to be able to re schedule all the alarms, you need to have them stored somewhere.

You can write your own ContentProvider for example.

  • It works well with other android components thanks to the CursorAdapter widget.
  • It is not the easiest solution but it's the way to go if you want to follow android guidelines.

There may be other simpler alternative to store your alarms, like SharedPreferences.

  • It's easy to use.
  • But you will need to hack around to store multiple alarms in a friendly manner.

One last alternative is that you can create an object containing the information, serialize it and store it as a file on the SD Card.

  • It's ugly and not flexible.
  • But it not that hard to implement ...

If you want to have a closer look to each storage options, you can read about it in the docs here : http://developer.android.com/guide/topics/data/data-storage.html

I hope all this help you. :)