Android 即使应用程序未运行,如何显示通知
我正在开发一个应用程序,我需要根据用户选择的时间间隔显示通知。即使应用程序关闭,我也必须显示这些通知。我已使用AlarmManager安排重复通知。当应用程序正在运行或处于“尝试”状态时,它工作正常,但如果应用程序被终止,它将不工作Android 即使应用程序未运行,如何显示通知,android,Android,我正在开发一个应用程序,我需要根据用户选择的时间间隔显示通知。即使应用程序关闭,我也必须显示这些通知。我已使用AlarmManager安排重复通知。当应用程序正在运行或处于“尝试”状态时,它工作正常,但如果应用程序被终止,它将不工作 public static void setRecurringNotification(Context context, long scTime) { System.out.println("Inside setRecurringNotification()
public static void setRecurringNotification(Context context, long scTime) {
System.out.println("Inside setRecurringNotification()");
Intent notificationIntent = new Intent(context,
NotificationReceiver.class);
pendingIntentNotification = PendingIntent.getBroadcast(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() + scTime, scTime,
pendingIntentNotification);
}// End of setRecurringAlarm()
下面是NotificationReceiver类:
public class NotificationReceiver extends WakefulBroadcastReceiver {
public static void sendNotification(Context context, MyMessage msg) {
System.out.println("sendNotification(): " + msg.getId());
Uri soundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(msg.getTitle())
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(
new NotificationCompat.BigTextStyle().bigText(msg.getContent()))
.setContentText(msg.getContent())
.setSound(soundUri);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
}// End of sendNotification
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Inside NotificationReceiver.onReceive...");
try {
new NotifyUser(context).execute();
}
catch (Exception ex) {
ex.printStackTrace();
}
setResultCode(Activity.RESULT_OK);
}
private class NotifyUser extends AsyncTask<Void, Void, String> {
private Context context;
public NotifyUser(Context context) {
this.context = context;
}
@Override
protected String doInBackground(Void... params) {
SQLHelper sqlHelper = new SQLHelper(context);
try {
MyMessage msg = sqlHelper.getRandomMyMessageFromDB();
if (null != msg)
sendNotification(context, msg);
}
catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
}
公共类NotificationReceiver扩展WakefulBroadcastReceiver{
公共静态void sendNotification(上下文上下文,MyMessage msg){
System.out.println(“sendNotification():”+msg.getId());
Uri soundUri=ringtonemager
.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationManager mNotificationManager=(NotificationManager)上下文
.getSystemService(上下文通知服务);
PendingEvent contentIntent=PendingEvent.getActivity(上下文,0,
新意图(上下文,MainActivity.class),0;
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(
(上下文)
.setSmallIcon(R.drawable.ic_启动器)
.setContentTitle(msg.getTitle())
.setSmallIcon(R.drawable.ic_启动器)
塞斯特尔先生(
新建NotificationCompat.BigTextStyle().bigText(msg.getContent())
.setContentText(msg.getContent())
.setSound(soundUri);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1,mBuilder.build());
}//发送结束通知
@凌驾
公共void onReceive(上下文、意图){
System.out.println(“内部NotificationReceiver.onReceive…”);
试一试{
新建NotifyUser(context.execute();
}
捕获(例外情况除外){
例如printStackTrace();
}
setResultCode(Activity.RESULT\u OK);
}
私有类NotifyUser扩展异步任务{
私人语境;
公共NotifyUser(上下文){
this.context=上下文;
}
@凌驾
受保护字符串doInBackground(无效…参数){
SQLHelper SQLHelper=新的SQLHelper(上下文);
试一试{
MyMessage msg=sqlHelper.getRandomMyMessageFromDB();
如果(null!=msg)
发送通知(上下文,msg);
}
捕获(例外情况除外){
例如printStackTrace();
}
返回null;
}
}
}
当应用程序关闭时,您必须使用Android服务执行后台操作。有关更多详细信息,请参阅我们是否需要广播接收器?实际上,我试着从NotificationReceiver类调用服务。但是它没有给出我期望的结果。如果必须根据系统事件执行某些操作,则必须使用BroadcastReceiver。否则服务就足够了。按照官方文件进行服务。很好地理解它,它就会起作用。数百万开发人员正在使用相同的方法。其余我会张贴一些链接,如果我会得到同样的例子。