【问题标题】:Activity starting Broadcast Receiver to check network status活动启动广播接收器以检查网络状态
【发布时间】:2016-11-24 04:04:09
【问题描述】:

我有一个正在初始化广播接收器的活动。我希望此广播接收器在收到手机网络状态已更改的消息后调用我的活动中的函数。

不幸的是,广播接收器似乎从未收到此消息,因为从未调用过 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>

【问题讨论】:

  • 我正在使用 API 25 对其进行测试

标签: android broadcastreceiver oncreate


【解决方案1】:

广播接收者也需要在你的manifest.xml中声明

添加

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

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

【讨论】:

    【解决方案2】:

    您不必在清单中注册接收者吗?像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多