活动启动广播接收器以检查网络状态

时间:2016-11-23 19:44:21

标签: android broadcastreceiver oncreate

我正在进行一项初始化广播接收器的活动。我希望这个广播接收器在收到手机网络状态发生变化的消息后,在我的活动中调用一个功能。

不幸的是,广播接收器似乎永远不会收到此消息,因为永远不会调用onReceive函数。

接收者:

private BroadcastReceiver NetworkReceiver= new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            final android.net.NetworkInfo wifi = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            final android.net.NetworkInfo mobile = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if (!wifi.isAvailable() || !mobile.isAvailable()) {
                Log.d("debug"," in the if");
                Handler handler = new Handler();
                handler.postDelayed(sendUpdatesToUI, 10);
                Log.d("Network Available ", "Flag No 1");
            }
        }
    };

    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            update();
        }
    };

    private void update() {
        Log.d("debug", "got update");
    }

我在onCreate函数中使用此行调用它:

 registerReceiver(NetworkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rtuya.secmere">

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBar" />

    </application>

</manifest>

修改

我已将此添加到我的清单中,但仍然没有结果:

<receiver
            android:name="NetworkReceiver"
            android:label="NetworkReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

2 个答案:

答案 0 :(得分:0)

广播接收器也需要在manifest.xml

中声明

添加

<receiver android:name=".ReceiverName" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
</receiver>

您可能希望将接收器放在其自己的类中,而不是匿名进行。

答案 1 :(得分:0)

你不必在舱单上登记接收人吗?像这样:

<receiver
        android:name="Receiver"
        android:label="Receiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
</receiver>