重新启动后不会触发警报 [英] Alarm doesn't trigger after reboot

查看:94
本文介绍了重新启动后不会触发警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个警报,说每15分钟重置一次数据连接.问题是,一旦手机重新启动,应用程序将被终止,并且警报(服务)不再触发.(这不是重复的,关于SO的其他类似问题也不能解决我的问题.)

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>

广播接收器:

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

触发警报的代码(在onClick上):

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

有什么建议吗?

推荐答案

阅读 AlarmManager 文档,其中写道, AlarmManager 在重启后将不保存警报.

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

根据 Android 开发人员

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.

关于 BOOT_COMPLETED ,他们写道:

... 系统已完成引导.它可用于执行特定于应用程序的初始化,例如安装警报.您必须拥有RECEIVE_BOOT_COMPLETED权限才能接收此广播.

... 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.

关于许可,您已经注册了.可能发生的事情是, BOOT_COMPLETED 服务正在与应用程序一起在本地使用.移动设备的 reboot 系统活动,它不会被 overrided 覆盖以完成对所保存警报的重新注册.但我不确定.因此,当执行 BOOT_COMPLETED 时,您需要做一些事情.

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.

我所做的是,我注册了所有警报,并使用 SQLite 形成了数据库;在 restart 上,我用于重置所有警报.那对我有用.如果您想按照我的程序进行操作,

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,

为您的警报创建数据库,并将其保存在该数据库中.以这样的方式编写您的应用程序:电话重启时, AlarmManager 会重置所有警报.这就是它的工作原理.

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.

在以下位置自动启动服务Android重新启动后.上面写着:

还要注意,从Android 3.0开始,用户需要至少启动一次应用程序,然后您的应用程序才能接收到android.intent.action.BOOT_COMPLETED事件.

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.

这篇关于重新启动后不会触发警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆