简体   繁体   English

当应用程序处于后台时,将数据从BroadcastReceiver发送到活动

[英]Send data from BroadcastReceiver to activity, while app is in background

I am writing my code in Android Studio, but the project that I am writing has no GUI, and never updates views for a user. 我正在Android Studio中编写代码,但是正在编写的项目没有GUI,并且永远不会为用户更新视图。 It simply listens for the last installed app with a Broadcast Receiver and sends info to another java file. 它只是使用广播接收器监听最后安装的应用程序,然后将信息发送到另一个Java文件。

I am wondering how I can send a string from a broadcast receiver to an activity (maybe this should not be an "activity" but a "service"?), and have this work while the app is in the background. 我想知道如何从广播接收器向活动发送字符串(也许这不应该是“活动”而是“服务”吗?),并且在应用程序处于后台时进行这项工作。

Right now I have a broadcast receiver listening for the last installed app on my phone (this runs in the background). 现在,我有一个广播接收器,正在侦听手机上最后安装的应用程序(此应用程序在后台运行)。 And I have a custom broadcast receiver set up to notify my main activity when the broadcast receiver gets an installed app. 我设置了一个自定义广播接收器,以在广播接收器安装了应用程序时通知我的主要活动。 But this custom receiver doesn't work in the background. 但是此自定义接收器无法在后台运行。 Again, maybe I shouldn't be using an Activity to receive info from the BroadcastReceiver? 同样,也许我不应该使用Activity从BroadcastReceiver接收信息?

Is there a way I can send information from my broadcast receiver to my main activity while the app is in the background? 在后台运行应用程序时,是否可以将信息从广播接收器发送到主要活动? I also need my main activity to resume normal function while in the background (there are no GUI updates done while it's in the background). 我还需要我的主要活动来在后台恢复正常功能(在后台时没有完成GUI更新)。

My BroadcastReceiver which is sending data to my Main Activity through a new Intent 我的BroadcastReceiver通过新的Intent向我的主活动发送数据

public class NewInstallReceiver extends BroadcastReceiver {

ApplicationInfo ai;

@Override
public void onReceive(Context context, Intent intent) {

    final PackageManager pm = context.getPackageManager();

    try {

        // if app is added, get package info

        ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);

        Intent i = new Intent("broadcastName");
        // Data pass to activity
        i.putExtra("appInfo", ai);
        context.sendBroadcast(i);

    } catch (final PackageManager.NameNotFoundException e) {
        ai = null;
    }
}

}

In my MainActivity in onCreate I register the receiver: 在onCreate的MainActivity中,我注册接收者:

registerReceiver(broadcastReceiver, new IntentFilter("broadcastName"));

I receive data from BroadcastReceiver 我从BroadcastReceiver接收数据

BroadcastReceiver broadcastReceiver =  new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        // get applicationInfo
        Bundle b = intent.getExtras();
        Object applicationInfo = b.get("appInfo");

        ApplicationInfo appInfo = (ApplicationInfo) applicationInfo;

        getNewData(appInfo);
    }
};

Then I have to unregister the receiver in MainActivity, but obviously this doesn't allow for me to receive info from BroadcastReceiver once the app goes in the background 然后,我必须在MainActivity中注销接收器,但是很明显,一旦应用程序进入后台,这显然不允许我从BroadcastReceiver接收信息。

@Override
public void onStop()
{

    try{

        unregisterReceiver(broadcastReceiver);
    }

    catch(Exception e){
        System.out.println("Exception is " + e.toString());
    }

    super.onStop();
}

Honestly, I don't think that update an Activity that is in background is a good practice (even if possible). 老实说,我认为更新处于后台的活动不是一个好习惯(即使可能)。

According to DOCS : 根据DOCS

An activity is a single, focused thing that the user can do. 活动是用户可以做的一件专注的事情。 Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). 几乎所有活动都与用户进行交互,因此Activity类负责为您创建一个窗口,您可以在其中放置带有setContentView(View)的UI。 While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows... or embedded inside of another activity (using ActivityGroup). 尽管活动通常以全屏窗口的形式呈现给用户,但它们也可以以其他方式使用:作为浮动窗口...或嵌入到另一个活动中(使用ActivityGroup)。

So, if the Activity is in background, it does not visible. 因此,如果“活动”在后台,则它是不可见的。 So, it is useless to update its contents (because it is not visible to user). 因此,更新其内容是无用的(因为它对用户不可见)。

In fact, you should implement following behavior instead: 实际上,您应该改为实现以下行为:

  • Use your Service only to save the information (in a database/content provider) and to notifies the Activity that a new information is available. 仅将您的服务用于保存信息(在数据库/内容提供程序中),并通知活动有新信息可用。 Then, if your activity is open, it immediately consumes that information. 然后,如果您的活动处于打开状态,它将立即使用该信息。 If Activity is not opened, the info remains saved for when the user needed it. 如果未打开“活动”,则该信息将保留,以备用户需要时使用。

  • If app was only stopped, you can update the Activity content during onResume() method. 如果仅停止了应用程序,则可以在onResume()方法期间更新“活动”内容。

  • If your app was killed and user opened it again, you can update ALL Views during onCreate() method. 如果您的应用被杀死并被用户再次打开,则可以在onCreate()方法期间更新所有视图。 onCreate() is called before the View is displayed to user. 在向用户显示视图之前,将调用onCreate()

This way, the info will be updated when needed: when the user wan's to check the info . 这样,信息将在需要进行更新: 当用户要检查信息时 Otherwise, it is a waste of time. 否则,那是浪费时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM