6

在我的清单文件中,我已声明接收者。 (如下)当应用程序未运行时BroadcastReceiver不工作

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

但是,一旦我关闭我的应用程序,我无法获得警报和通知。显然,我的Broadcast receiver中的OnReceive电话永远不会发出。

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

在MainActivity内部,我的报警管理器类如下所示。

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> 

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

回答

0

你应该增加意图过滤器清单中,作为

receiver android:name=".SmsBroadCastReceiver"> 
     <intent-filter android:priority="20"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
     </intent-filter> 
    </receiver> 
+1

报警接收是否一样?因为你给了短信 – tony9099 2013-05-06 08:58:38

0

由于龚聪说,你需要声明你的接收机应该听哪些事件。

例如:

<receiver android:name=".OnAlarmReceive"> 

<intent-filter> 
    <action android:name="MY_ALARM_NOTIFICATION"/> 
</intent-filter> </receiver> 

,然后当你设置报警,使用目的与您的行动:

Intent intent = new Intent("MY_ALARM_NOTIFICATION"); 
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); 
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); 
+0

我做到了。 但是,一旦我关闭我的活动,警报不会熄灭。与应用程序运行上面的代码(即使在我的更改后)工作。但是当我从应用程序管理器中删除应用程序时,它不会运行。 – tony9099 2013-05-06 09:10:20

+0

你能发表你的部分代码吗?你如何设置你的闹钟?你使用AlarmManager.RTC_WAKEUP标志吗? – 2013-05-06 09:16:53

+0

是的,我这样做,但是像这样,我不能把意图额外的,因为我不是用意图调用一个活动,而是在清单中的广播接收器没有? – tony9099 2013-05-06 09:27:19

0

您的代码工作正常!

所有你所要做的就是改变这一行:

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

这一行:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
SystemClock.elapsedRealtime() + 5000, pendingIntent); 

而在“的onReceive”代码将5000毫秒后运行(5秒),即使应用程序没有运行

-1

1.Declare接收器在清单文件:

<receiver android:name="your.package.name.TestAlarmReceiver"></receiver> 

请务必记住整个Android系统区分大小写。所以在AndroidMainfest.xml中检查拼写是否正确。

2.如果您为Receiver创建了PendingIntent,请添加requestCode - 即使它是随机数!没有你的onReceive代码永远不会被调用!

其启动AlarmManager看起来应该像下面的功能:

public static void scheduleTestAlarmReceiver(Context context) { 
    Intent receiverIntent = new Intent(context, TestAlarmReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0); 

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+startDelay, someDelay, sender); 
} 

广播接收器类:

package your.package.name; 

public class TestAlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent arg1) { 
    // your code here! 
    } 
} 

原条款:Why my BroadcastReceiver does not get called?

0

在我的理解,在一些套管取决于实施方式,OS有权调整警报设置时间。因此,请尝试相应地使用AlarmManager.set(...),AlarmManager.setexact(...)等。在某些情况下,根据制造商(自定义Android操作系统),操作系统可能会阻止火警。

0

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

相关问题