用于测试网络状态的广播接收器不工作

时间:2016-05-23 01:08:22

标签: android

我试图通过BroadcastReceiver检查wifi和移动设备的互联网连接。 Toast应显示连接更改。 我注册了接收器,但它没有显示变化。

    package com.axa.conetividad;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    //Muestra si el display debe de ser refrescada
    public static boolean availableConnection = false;
    public boolean isReceiverRegistered = false;

    NetworkReceiver networkReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        networkReceiver = new NetworkReceiver();
        registerReceiver();
    }


    @Override
    public void onResume() {
        super.onResume();
        registerReceiver();
    }

    @Override
    public void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(networkReceiver);
        isReceiverRegistered = false;
    }

    private void registerReceiver() {
        if (!isReceiverRegistered) {
            //Registra el BroadCastReceiver para hacer el track de la red
            IntentFilter filter = new IntentFilter();
            filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            LocalBroadcastManager.getInstance(this).registerReceiver(networkReceiver, filter);
            isReceiverRegistered = true;
        }
    }


    public class NetworkReceiver extends BroadcastReceiver {

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

            //Monitoreamos la conexión
            ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = conn.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected()) {
                availableConnection = true;
                Toast.makeText(context, " wifi", Toast.LENGTH_SHORT).show();

            } else if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE && networkInfo.isConnected()) {
                availableConnection = true;
                Toast.makeText(context, "mobile", Toast.LENGTH_SHORT).show();

            } else {
                availableConnection = false;
                Toast.makeText(context, "No internet connection", Toast.LENGTH_SHORT).show();
            }

        }
    }

}

这是我的清单。

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

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

1 个答案:

答案 0 :(得分:1)

使用您活动的registerReceiver()方法,

因此请更改此行

LocalBroadcastManager.getInstance(this).registerReceiver(networkReceiver, filter);

registerReceiver(networkReceiver,filter);

并且不要忘记在 onPause()方法中取消注册您的接收器

@Override
 protected void onPause() {
  if (networkReceiver != null) {
   unregisterReceiver(networkReceiver);
   networkReceiver = null
  }
  super.onPause();
 }