Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我的应用程序处于后台时,如何显示通知?_Java_Android_Notifications_Background_Broadcastreceiver - Fatal编程技术网

Java 当我的应用程序处于后台时,如何显示通知?

Java 当我的应用程序处于后台时,如何显示通知?,java,android,notifications,background,broadcastreceiver,Java,Android,Notifications,Background,Broadcastreceiver,​我正在尝试构建一个具有- 第一个活动(启动器活动) 和第二个活动 我正在努力实现的目标: 第一个Activity接受用户的输入,使用AsyncTaskLoader从web获取数据。 在onLoadFinished中,它检查输入是否有效,以及输入是否有效,是否启动了一个向sh发送第二个活动的意图​​还有更多细节。输入也随意图一起传递 由于网络操作可能需要时间,因此我决定在创建第二个活动之前使用IntentService显示通知,即在Intent之前在onLoadFinished中调用通知。如果第

​我正在尝试构建一个具有-

第一个活动(启动器活动)

和第二个活动

我正在努力实现的目标:

第一个Activity接受用户的输入,使用AsyncTaskLoader从web获取数据。 在onLoadFinished中,它检查输入是否有效,以及输入是否有效,是否启动了一个向sh发送第二个活动的意图​​还有更多细节。输入也随意图一起传递

由于网络操作可能需要时间,因此我决定在创建第二个活动之前使用IntentService显示通知,即在Intent之前在onLoadFinished中调用通知。如果第二个活动尚未启动,则通知应直接启动该活动

什么工作正常:

活动和装载机都工作正常。通知也会显示出来,只要用户保持应用程序运行,一切正常

什么不是:

当用户在加载数据时离开第一个活动时,不会显示通知,因为执行了onPause。我试着搜索它,了解了BroadcastReceiver、JobScheduler和FirebaseJobDispatcher,但我不能真正理解它们以及它们是如何帮助的

那么,我走对了吗?即使应用程序进入后台,我如何实现相同的功能


另外,代码比问题所暗示的要广泛得多,所以我没有包括它。这就是为什么如果解释不完整或混乱,我会尽力澄清您的疑问。

是的。你在正确的轨道上

这是我制作的地图下载器的一个示例。我希望你不介意,但由于你没有给出背景,我不得不找一个合适的例子

/**
* @param entry the map entry to be downloaded
* @return returns a receiver that unzips the map file after downloading
*/
private BroadcastReceiver createMapReceiver(MapEntry entry) {
     return new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
             unregisterReceiver(this); // we unregister this receiver after the download is complete
             receiveDownloadedFile(entry); // once we receive the map file, we run this function, but you can do whatever you want. This is the part of the code youn were asking for
         }
     };
}


void downloadMap() {

    // preparing the download
    BroadcastReceiver receiver = createMapReceiver(entry); // see function above

  DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
  registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

  String downloadURL = fileListURL + entry.getFileName() + fileExtension; // building the url for the map
  DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));

  if (manager != null) {
      manager.enqueue(request
              .setDestinationUri(Uri.fromFile(new File(areaFolder, entry.getFolderName())))
                    .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false)
                    .setTitle("Some title, preferably taken from Strings.xml")
                    .setDescription("Some Description")
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            );
        } else {
            // if we couldn't find the DownloadManager
            onFailure(entry); // we simply run some onFailure function
        }

    }

不要忘记权限。

通过LocalBroadcastManager注册广播接收器,并定义筛选器。现在,根据过滤器,您可以使用UI元素或意图

IntentFilter filter = new IntentFilter();
    filter.addAction("A");
    filter.addAction("B");
    filter.addAction("C");
    LocalBroadcastManager.getInstance(getContext()).registerReceiver(dummyReceiver, filter);



private BroadcastReceiver dummyReceiver = new BroadcastReceiver() {
@Override
    public void onReceive(final Context context, final Intent intent) {
          //Do your work**
 }}
试试这个。我已经实现了这一点,它是有效的

即使应用程序进入后台,我如何实现相同的功能

使用或。 意图服务允许您异步执行操作。 绑定服务可以使用提供接口。只要服务被绑定,它就不太可能被破坏。使用标志绑定服务有多种方法

依我看,活动只是一种显示UI的方式。如果您需要任何背景工作,请使用服务。
通过aidl使用绑定服务时,您必须扩展。绑定后,它将从服务处接收接口。

我最终使其在不使用广播接收器或服务的情况下工作,所以不熟悉广播接收器或服务的人可以参考此接口,但请自行决定使用它,因为服务和接收器是推荐的方法

@Override
public void deliverResult(Data data) {
    super.deliverResult(data);
    showNotification(input);
}
这是AsyncTaskLoader中的一种方法,在获取数据后触发。覆盖它并更改响应通知单击的通知方法

private PendingIntent onClick(Context context) {
     return PendingIntent.getActivity(context, 0, new Intent(context, ExtraActivity.class).
putExtra(Intent.EXTRA_TEXT,input),PendingIntent.FLAG_UPDATE_CURRENT);
}
这种外向性类似于。它允许您的通知响应,即使活动已关闭,但通知尚未关闭


同样,这只是针对这种特定情况的一种变通方法,这导致我在许多地方传递输入变量,因此它可能不如其他选项那样高效。但如果我在AsyncTaskLoader的onLoadFinished中注册它,当活动处于后台时,它将不会被执行,我需要根据返回的数据验证输入。那么我应该在哪里注册接收器呢?如果您需要做的事情太复杂,那么您应该将所有这些行为封装在一个服务中。服务正是这样:即使应用程序被隐藏,也可以在后台运行的代码。请看一下文档。如果你在那之后还需要帮助。我很乐意帮你,投赞成票。但如果我在AsyncTaskLoader的onLoadFinished中注册它,当活动处于后台时,它将不会被执行,我需要根据返回的数据验证输入。那么我应该在哪里注册接收者呢?如果你有一些按钮点击或者需要采取一些行动,那么就在行动上注册接收者,并执行你的异步任务以及其中的一切。