在Android中关闭应用程序后,从后台运行的服务发送通知

在Android中关闭应用程序后,从后台运行的服务发送通知,android,notifications,Android,Notifications,我制作了一个应用程序,它使用服务与服务器通信。来自服务的响应用于更新UI。我正在使用broadcastReceiver来执行此操作。当我按下back按钮时,服务将继续在后台运行(我不是在主线程上运行服务)。现在后退按钮关闭我的应用程序。现在我想从服务向用户发送关于响应的通知 有什么建议吗?通知: NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVI

我制作了一个应用程序,它使用
服务
与服务器通信。来自服务的响应用于更新UI。我正在使用
broadcastReceiver
来执行此操作。当我按下back按钮时,服务将继续在后台运行(我不是在主线程上运行服务)。现在后退按钮关闭我的应用程序。现在我想从服务向用户发送关于响应的通知

有什么建议吗?

通知:

  NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                Notification.Builder builder = new Notification.Builder(
                        getApplicationContext());

                Intent intent = new Intent(getApplicationContext(),
                        YourActivity.class);
                intent.setAction("notification");
                PendingIntent pendingIntent = PendingIntent.getActivity(
                        getApplicationContext(), 0, i    intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

                builder.setSmallIcon(R.drawable.app_icon)
                        .setContentTitle("Next Appoinment....")
                        .setContentText(response)
                        .setTicker("Ticker")
                        .setLights(0xFFFF0000, 500, 500)
                        // setLights (int argb, int onMs, int offMs)
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true);

                Notification notification = builder.getNotification();
                notificationManager.notify(R.drawable.app_icon,
                        notification);

您必须采取的方法是,您必须在SharedReferences中维护布尔变量,它将告诉您应用程序的状态,即一旦创建,您将填充该变量设置为true,当按下back按钮时,将调用onDestroy(),在此方法中,您将布尔变量设置为false。现在,当您的服务返回时,您必须检查该布尔变量。如果该值为true,则可以使用广播通知UI,如果变量为false,则应使用srikanth共享的代码gr@Rollno1在服务类或我的主要活动中,我应该将sriKanth_gr.建议的代码放在哪里?@AbhishekBatra:如果我正确理解您的问题。您必须在服务中编写逻辑。我是否需要声明
intent.setAction(“通知”)在我的清单中?像