应用程序未运行时 BroadcastReceiver 不工作 [英] BroadcastReceiver not working when app is not running

查看:51
本文介绍了应用程序未运行时 BroadcastReceiver 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的清单文件中,我已经声明了接收者.(如下)

In my manifest file I have declared the receiver. (as follows)

<receiver android:name=".OnAlarmReceive" />

但是,一旦我关闭了我的应用程序,我就无法收到警报和通知.显然,在我的 Broadcast Receiver 中从未调用过 OnReceive.

however, once I shut down my application, I am not able to get the alarms and the notifications. Apparently, a call to the OnReceive in my Broadcast receiver is never made.

public class OnAlarmReceive extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent arg1)
   {
       //various stuff
   }
}

在MainActivity里面,我的报警管理器类如下

Inside the MainActivity, my alarm manager class is as the follows.

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent("MY_ALARM_NOTIFICATION");
    intent.setClass(this, OnAlarmReceive.class);
    intent.putExtra("message", message);
    PendingIntent pendingIntent = PendingIntent
            .getBroadcast(MainActivity.this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar timeCal = Calendar.getInstance();
    timeCal.set(Calendar.HOUR_OF_DAY, hour);
    timeCal.set(Calendar.MINUTE, minutes);

    alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent);

我的清单如下:

    <receiver android:name=".OnAlarmReceive">
    <intent-filter android:priority="1">  
        <action android:name="MY_ALARM_NOTIFICATION"/>  
    </intent-filter>  
</receiver>  

即使我关闭了我的应用程序,我应该怎么做才能收到通知/警报.后台服务?

What should I do in order to receive the notifications/alarms even if I have shut off my app. Background service ?

推荐答案

在清单文件中为接收器添加 android:exported="true" 帮助我接收警报(从而唤醒应用程序)即使应用程序被关闭(我故意从任务列表中删除应用程序).

Adding android:exported="true" for receiver in manifest file helped me to receive alarms (and thus, wake the application) even when application was shut-down (intentionally by me, removing app from task list).

这篇关于应用程序未运行时 BroadcastReceiver 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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