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

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

问题描述

我有一个报警重置数据连接说每隔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

code到火灾报警(上的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.

根据在 的Andr​​oid 开发者

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 ,他们写道:

... 这是广播一次,后系统完成启动。它可以被用来执行特定应用的初始化,如安装报警。您必须持有允许项值才能收到该广播。

... 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 正在本地使用,与应用程序。在重新启动移动是的系统活动,不是的重写的实现报警的重新登记被保存。但我不知道。所以,你需要做一些事情的时候执行进来的手 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 ;在重新启动,我用它来重置所有报警。嗯,这为我工作。如果你想要去与我的过程的话,

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.

自动启动的服务再次的Andr​​oid开机后。这是写:

另外请注意,由于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天全站免登陆