android – AlarmManager在断电时删除所有时间表?

android – AlarmManager在断电时删除所有时间表?,第1张

概述我在特定时间设置了闹钟.如果手机关机,闹钟就会丢失?我已关闭手机,再次启动,但闹钟未在指定时间触发. PendingIntent pendingIntent = PendingIntent.getBroadcast( Global.a.getApplicationContext(), (int) cd.e.alarmTime, intent, 0); AlarmM 我在特定时间设置了闹钟.如果手机关机,闹钟就会丢失?我已关闭手机,再次启动,但闹钟未在指定时间触发.

PendingIntent pendingIntent = PendingIntent.getbroadcast(            Global.a.getApplicationContext(),(int) cd.e.alarmTime,intent,0);    AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE);    alarmManager.set(AlarmManager.RTC,cd.e.alarmTime,pendingIntent);
解决方法 首先,您需要创建一个broadcastReceiver以在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());            }    }}

然后创建一个服务以重置所有警报:

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,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);    }}

最后,将权限和接收者添加到清单中

<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>
总结

以上是内存溢出为你收集整理的android – AlarmManager在断电删除所有时间表?全部内容,希望文章能够帮你解决android – AlarmManager在断电时删除所有时间表?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1122881.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存