在我的清单文件中,我已经声明了接收者。(如下)
<receiver android:name=".OnAlarmReceive" />
但是,一旦我关闭了我的应用程序,我就无法获得警报和通知。显然,从未拨打OnReceive
过我的电话。Broadcast receiver
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>
即使我关闭了我的应用程序,我应该怎么做才能接收通知/警报。后台服务?