gpt4 book ai didi

android - 重启后不触发警报

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:41 24 4
gpt4 key购买 nike

我有一个警报,要求每 15 分钟重置一次数据连接。问题是,一旦手机重启,应用程序就会被杀死,警报(服务)也不会再触发。(这不是重复的,SO上的其他类似问题没有解决我的问题。)

 <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 上):

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重启后不会保持报警。

根据 Android 开发人员

.... 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 正在本地与应用程序一起使用。手机的重启系统 Activity ,它不会被覆盖以完成已保存警报的重新注册。但我不确定。因此,当 BOOT_COMPLETED 即将执行时,您需要做一些事情。

阅读Automatically starting Services in Android after booting ,这可能会对您有所帮助。

我所做的是,我使用 SQLite 注册了所有警报并形成了一个数据库;在 restart 上,我用来重置所有警报。那对我有用。如果你想继续我的流程,那么,

为您的警报创建一个数据库,并将它们保存在那里。以这样的方式编写您的应用程序,当手机重新启动时,AlarmManager 会重置所有警报。这就是它的工作原理。


Automatically starting Services in Android After booting再次。是这样写的:

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.

关于android - 重启后不触发警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com