重新启动后,警报管理器不再重复 [英] Alarm Manager isn't repeating after reboot

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

问题描述

我的警报管理器有问题。我创建了警报管理器,每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); 

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

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