通过广播实现网络监听及判断网络类型


权限设置:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class BroadCastActivity extends AppCompatActivity {
    private Context context;
    private IntentFilter intentFilter;
    private NetworkChangeReceiver networkChangeReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_cast);

        intentFilter = new IntentFilter();//创建实例
        intentFilter.addAction("android.net.CONNECTIVITY_CHANGE");//添加了一个action 接收指定的广播
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);//动态注册的广播一定要注销
    }

    private class NetworkChangeReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"网络变化",Toast.LENGTH_SHORT).show();
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if(networkInfo != null && networkInfo.isAvailable()){
                Toast.makeText(context,"网络不可用",Toast.LENGTH_SHORT).show();
                gotoSet();
            }else{
                Toast.makeText(context,"网络可用",Toast.LENGTH_SHORT).show();
                //判断是WIFI还是数据连接
                int infotype0 = networkInfo.getType();
                int infotype1 = networkInfo.getSubtype();
                if(infotype0 == ConnectivityManager.TYPE_WIFI){
                    Toast.makeText(context,"现在是WIFI连接",Toast.LENGTH_SHORT).show();
                }else if(infotype0 == ConnectivityManager.TYPE_MOBILE){
                    Toast.makeText(context,"现在是手机数据流量连接,请酌情使用",Toast.LENGTH_SHORT).show();
                }else {
                    //还有其他类型的连接 具体看API
                }
            }
        }
    }
    private void gotoSet() {
        AlertDialog.Builder builder = new AlertDialog.Builder(BroadCastActivity.this);
        builder.setMessage("网络不可用,请设置")
                .setTitle("提示")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent =  new Intent(Settings.ACTION_SETTINGS);
                        startActivity(intent);
                        context.startActivity(intent);
                        }
                    })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                        }
                    })
                .create()
                .show();
    }
}

你可能感兴趣的:(Android)