AlarmManager delete all schedules on power-off?

前端 未结 3 482
無奈伤痛
無奈伤痛 2021-01-06 14:16

I have set an alarm at specific time. If the phone is turn off the alarm is lost? I have turn off the phone, start it again but the alarm did not trigger at the specified ti

相关标签:
3条回答
  • 2021-01-06 14:30

    You could do it like this:

    Store the time in a SharedPreference, catch Intent.ACTION_BOOT_COMPLETED and set the AlarmManager again if required.

    0 讨论(0)
  • 2021-01-06 14:32

    Tried @Woodsy Method but didn't work so i fixed some things so it can work

    public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
    
            Log.d("receiver", "The Receiver is called");
            Intent mServiceIntent = new Intent(context, BootService.class);
            context.startService(mServiceIntent);
    
        } else {
            Log.d("boot receiver ",
                    "Something went wrong with the boot complete maybe with permissions ");
        }
    }
    

    }

    *Permissions & Manifest *

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    
    
    Booting update 
            <service android:name="com.packagename.appname.service.BootService" />
    
            <receiver
                android:name=".Receivers.BootReceiver"
                android:enabled="true" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" >
                    </action>
                </intent-filter>
           </receiver>
    

    Hope this could help others

    0 讨论(0)
  • 2021-01-06 14:42

    First you need to create a BroadcastReceiver to trigger on BOOT_COMPLETE

    public class BootReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(final Context context, final Intent intent) {
                if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                        Intent mServiceIntent = new Intent();
                        mServiceIntent
                                        .setAction("com.crossfire.appname.service.BootService");
                        ComponentName service = context.startService(mServiceIntent);
                        if (null == service) {
                                // something really wrong here
                                Log.e(TAG, "Could not start service ");
                        }
                } else {
                        Log.e(TAG, "Received unexpected intent " + intent.toString());
                }
        }
    }
    

    Then create a service to reset all your alarms:

    public class BootService extends Service {
    
        private final String TAG = "BootService";
    
        @Override
        public IBinder onBind(final Intent intent) {
                return null;
        }
    
        @Override
        public void onCreate() {
                super.onCreate();
                Trace.i(TAG, "created");
        }
    
        @Override
        public void onStart(final Intent intent, final int startId) {
                super.onStart(intent, startId);
                Trace.i(TAG, "started");
    
                LoadAlarmsFromDatabase();
    
        }
    
        private void LoadAlarmsFromDatabase() {
               Cursor c = mDbHelper.getPendingAlarms();
                if (c.moveToFirst()) {
                        do {
    
                                int id = c.getInt(mDbHelper.ID_COLUMN);
                                String time = c.getString(mDbHelper.TIME_COLUMN);
                                addNotification(id, time);
                        } while (c.moveToNext());
                }
                c.close();
        }
    
        private void addNotification(int apptId, String time) {
                Trace.d(TAG, "addNotification " + apptId + ", " + time);
                Intent intent = new Intent(BootService.this,
                                ApptBroadcastReceiver.class);
    
                intent.putExtra("appt_id", Integer.toString(apptId));
    
                PendingIntent mAlarmSender = PendingIntent.getBroadcast(
                                BootService.this, apptId, intent, 0);
    
                long alarmTime = System.currentTimeMillis() + 60000;
                Calendar c = null;
    
                try {
                        c = CustomDateUtils.StringToCalendar(time);
                        alarmTime = c.getTimeInMillis();
                } catch (ParseException e) {
                        Log.e(TAG, "ParseException");
                }
    
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, alarmTime, mAlarmSender);
    
        }
    }
    

    Finally, add the permission and receiver to the manifest

     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <receiver android:name=".receiver.BootReceiver" android:enabled="true">
                <intent-filter>
                        <action android:name ="android.intent.action.BOOT_COMPLETED"></action>
                </intent-filter>
        </receiver>
    
    0 讨论(0)
提交回复
热议问题