private class MyRecever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("
com.bawei.net
")){
String net = intent.getStringExtra("net");
Toast.makeText(MainActivity.this, "接收广播成功:"+net, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("
")){
String net = intent.getStringExtra("net");
Toast.makeText(MainActivity.this, "接收广播成功:"+net, Toast.LENGTH_SHORT).show();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//使用自己编写的工具类,判断是否是手机流量
boolean mobile = NetWorkUtils.isMobile(this);
//有网做对应的操作
if (mobile) {
Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show();
//无网络时,跳转网络设置界面
Intent intent = new Intent("
com.bawei.net
");
intent.putExtra("net", "亲,断网了,应该去设置网络了");
sendBroadcast(intent);
Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(wifiSettingsIntent);
}
boolean mobile = NetWorkUtils.isMobile(this);
//有网做对应的操作
if (mobile) {
Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show();
//无网络时,跳转网络设置界面
Intent intent = new Intent("
");
intent.putExtra("net", "亲,断网了,应该去设置网络了");
sendBroadcast(intent);
Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(wifiSettingsIntent);
}