重新启动后,警报管理器不再重复

编程入门 行业动态 更新时间:2024-10-22 23:36:58
本文介绍了重新启动后,警报管理器不再重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的警报管理器有问题。我创建了警报管理器,每15秒重复显示一次吐司。

I have problem with Alarm Manager. I create alarm manager which repeat displaying toast every 15 seconds.

重新启动设备后,吐司可见,但只有一次。即使重新启动后,我也想每15秒重复一次。

After rebooting my device, toast is visible, but only once. I want to repeat it again every 15 seconds even after reboot.

我可以添加些什么来解决此问题?

What can I add to solve this? Is this possible?

这是我的代码(AlarmReceiver类扩展了BroadcastReceiver):

Here is my code (class AlarmReceiver extends BroadcastReceiver):

@Override public void onReceive(Context context, Intent intent) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG"); //Acquire the lock wl.acquire(); Toast.makeText(context, "wow", Toast.LENGTH_LONG).show(); //Release the lock wl.release(); } public void SetAlarm(Context context) { AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15000, pi); }

还有我的AndroidManifest.xml

And my AndroidManifest.xml

<receiver android:name=".view.activity.AlarmReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver>

编辑:此问题的解决方法是在onReceiver()中编辑代码:

The solve of this problem is edit code in onReceiver():

@Override public void onReceive(Context context, Intent intent) { if(intent.getAction()==null){ Toast.makeText(context, "lol", Toast.LENGTH_LONG).show(); } else { AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent1 = new Intent(context, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent1, 0); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15000, pi); } }

推荐答案

似乎您只需要在onReceive方法中调用SetAlarm函数,并在清单中侦听已发送的事件。

It seems like you simply need to call your SetAlarm function in the onReceive Method, and listen for the sent event in your manifest.

在清单中

<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> //New <action android:name="com.packagename.custombroadcast" /> </intent-filter>

您的意图

Intent intent = new Intent(); intent.setAction("com.packagename.custombroadcast"); //Use Context.sendBroadcast sendBroadcast(intent);

更多推荐

重新启动后,警报管理器不再重复

本文发布于:2023-11-27 03:39:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报   重新启动

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!