Java 从活动中访问应用程序广播接收器?

Java 从活动中访问应用程序广播接收器?,java,android,broadcastreceiver,Java,Android,Broadcastreceiver,我在清单中设置了广播,以监控连接活动: <application ... <receiver android:name="com.blah.appname.NetworkChangeReceiver" android:label="NetworkChangeReceiver" > <intent-filter> <action android:name="android.

我在清单中设置了广播,以监控连接活动:

<application
    ... 
    <receiver
        android:name="com.blah.appname.NetworkChangeReceiver"
        android:label="NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
    ...
</application>
然而,我也需要在个人活动中根据失去联系的情况做一些特殊的事情


如何从活动中访问现有广播接收器的onReceive()方法?我不完全理解从清单中如何将接收器绑定到应用程序的内部结构。

您在应用程序中创建广播接收器,以便在应用程序中访问它。清单中的广播接收器必须在10秒内返回,因此通常,广播接收器的清单版本只会有目的地启动某些内容,然后返回

下面是一个向应用程序添加广播接收器的代码示例

 BroadcastReciver yourReceiver;


 public void onStart() {
    super.onStart();
    setupGPS();
 }


private void setupGPS() {
    if (yourReceiver == null) {
        // INTENT FILTER FOR GPS MONITORING
        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION_GPS);
        yourReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null) {
                    String s = intent.getAction();
                    if (s != null) {
                        if (s.equals(ACTION_GPS)) {
                            gpsCheck();
                        }
                    }
                }
            }
        };
        getActivity().registerReceiver(yourReceiver, theFilter);
    }
    gpsCheck();
}


private void gpsCheck() {
    if (view != null) {
        LinearLayout llTrackerEnableGPS = (LinearLayout) view
                .findViewById(R.id.llTrackerEnableGPS);
        if (llTrackerEnableGPS != null) {
            LocationManager locationManager = (LocationManager) fragmentActivity
                    .getSystemService(Context.LOCATION_SERVICE);
            boolean isGPSAvailable = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            llTrackerEnableGPS.setVisibility(isGPSAvailable ? View.GONE
                    : View.VISIBLE);
        }
    }
}



@Override
public void onPause() {
    super.onPause();
    if (yourReceiver != null) {
        final FragmentActivity a = getActivity();
        if (a != null) {
            a.unregisterReceiver(yourReceiver);
        }
    }
    yourReceiver = null;
}

因此,您想根据您在BroadcastReceive的
onReceive
中接收到的内容在您的活动中做些什么?以下是我以前做过的一些事情:

您可以使用
LocalBroadcast
。看见 关于如何实现is,有一个很好的例子

LocalBroadcast Manager是一个帮助程序,用于注册意图广播并将其发送到进程中的本地对象。你正在广播的数据不会离开你的应用程序,因此不必担心泄露私人数据

您的活动注册此本地广播。在
networkchangerecever
中,您从
onReceive
中发送
LocalBroadcast
(表示嘿,我收到了一条消息。显示它活动)。然后在
活动中
可以收听广播。这样,如果活动处于最前端/处于活动状态,它将接收广播,否则它不会接收广播。因此,每当您收到本地广播时,如果活动处于打开状态,您可以执行所需的操作

如果您想为整个应用程序执行此操作,则可以将所有活动扩展为抽象活动。在这个抽象活动类中,您可以为这个“LocalBroadcast”注册它。另一种方法是在所有活动中注册LocalBroadcast(但之后您必须管理如何只显示一次消息)。此广播对您的应用程序是私有的。所以它也是安全的


希望能有帮助。同样的事情也可以在
服务中完成。这只是其中一种方法。

您需要所谓的动态广播接收器。您不需要在清单文件中注册它,而是在活动中注册它

NetworkChangeReceiver mReceiver = new NetworkChangeReceiver();

@Override
protected void onStart() {
    super.onStart();
    registerReceiver(mReceiver, new IntentFilter(android.net.conn.CONNECTIVITY_CHANGE));
}

@Override
protected void onStop() {
    unregisterReceiver(mReceiver);
    super.onStop();
}
现在,您可以将逻辑添加到
onReceive()
分析连接状态并调整逻辑

或者,如果您想使用一些库,那么您可能想看看开源。这让一切变得简单多了。在您的活动中,您需要添加以下内容

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    // wire connectivity events
    TinyBus.from(this).wire(new ConnectivityEvents());
}


如果你从听者内部广播一个意图,然后从活动中订阅该自定义意图,会怎么样?非常好的例子,非常感谢。“你名单上的广播接收者必须在10分钟内返回”我似乎找不到记录在案的。。你有更多的信息吗?我的坏消息是10秒,我会编辑这篇文章。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    // wire connectivity events
    TinyBus.from(this).wire(new ConnectivityEvents());
@Subscribe
public void onConnectivityEvent(ConnectionChangedEvent event) {
    if (event.isConnected()) {
        // connected
    } else {
        // disconnected
    }
}