Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
750 views
in Technique[技术] by (71.8m points)

android - Alarm doesn't trigger after reboot

I have an alarm to reset a data connection say every 15 minutes. The problem is, once the phone is rebooted, the application gets killed and the alarm (service) doesn't trigger anymore. (This is not a duplicate, the other similar questions on SO do not solve my problem.)

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <receiver
        android:name="com.sang.mobiledata.ResetBroadcastReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

    </receiver>

Broadcast Receiver:

public void onReceive(Context context, Intent intent) {
    // if(CONN_ACTION.equals(intent.getAction())) {
    if (intent.getAction().equalsIgnoreCase(
            "com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE")) {
        MainActivity objMain = new MainActivity();

        objNetwork.setMobileDataEnabled(context, false);
        objNetwork.setMobileDataEnabled(context, true);

    }

    if (intent.getAction().equalsIgnoreCase(
            "android.intent.action.BOOT_COMPLETED")) {

            // code to restart/resume/retain alarm

Code to fire alarm (on the onClick):

Intent myIntent = new Intent(
                    "com.sang.mobiledata.IntentAction.RECEIVE_RESETCONN_UPDATE");
            myIntent.putExtra("FLAG_KEY", false);
            PendingIntent pi = PendingIntent.getBroadcast(this, 0, myIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) this
                    .getSystemService(Context.ALARM_SERVICE);

            calendar.setTimeInMillis(System.currentTimeMillis());

            calendar.add(Calendar.SECOND, 10);
            long interval = intHrs * 3600000 + intMins * 60000;
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), interval, pi);
            long mins = interval / 60000;
            Toast.makeText(
                    MainActivity.this,
                    "Data Connection will be reset every " + mins
                            + " minute(s).", Toast.LENGTH_SHORT).show();
            }

Any suggestions please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Read the AlarmManager document, there it is written, that AlarmManager will not hold alarms after reboot.

As per the Android Developers,

.... Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

And regarding BOOT_COMPLETED, they write:

... This is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization, such as installing alarms. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.

As for permission is concerned, you have already registered that. Well what probably happening is, the service BOOT_COMPLETED is being used locally, with the application. The reboot of the mobile is system activity, which is not overridden to accomplish re-registry of the alarms being saved. But I am not sure. So, you need to do something when execution comes in hand of BOOT_COMPLETED.

Read Automatically starting Services in Android after booting, this might help you with it.

What I did was, I registered all the alarms and formed a database, using SQLite; On restart, I use to reset all the alarms. Well that worked for me. If you want to go with my process then,

Make a database for your alarms, and save them there. Write your application in such a way that, when the phone restarts, the AlarmManager resets all the alarms. This is how it works.


In Automatically starting Services in Android After booting again. It is written:

Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...