android网络判断
来源:互联网 发布:mac dps文件怎么打开 编辑:程序博客网 时间:2024/05/29 04:38
1.myReceiver.java 广播
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (isNetworkConnected(context)) { int type = getConnectedType(context); switch (type) { case -1: Toast.makeText(context, "无有网络连接!请手动连接网络", Toast.LENGTH_SHORT) .show(); break; case 0: Toast.makeText(context, "已经接入手机网络,请注意流量!", Toast.LENGTH_SHORT) .show(); break; case 1: Toast.makeText(context, "WIFI网络", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(context, "wap网络", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(context, "net网络", Toast.LENGTH_SHORT).show(); break; default: break; } } else { Toast.makeText(context, "无网络连接!请手动连接网络", Toast.LENGTH_SHORT).show(); } } public boolean isNetworkConnected(Context context) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } return false; } public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; }}
2.在入口activity.java注册广播
public class MainActivity extends Activity { TextView textView; Button button; IntentFilter intentFilter; MyReceiver myReceiver = new MyReceiver(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } public void initView() { textView = (TextView) findViewById(R.id.tv); button = (Button) findViewById(R.id.btn); intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); //为BroadcastReceiver指定action,使之用于接收同action的广播 //registerReceiver(myReceiver, intentFilter); Intent service = new Intent(this, MyReceiver.class); startService(service); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (isNetworkConnected(getApplicationContext())) { // -1:没有网络 0:手机网络 1:WIFI网络 2:wap网络 3:net网络 int type = getConnectedType(getApplicationContext()); switch (type) { case -1: textView.setText("无网络接入" + type); Toast.makeText(getApplicationContext(), "无有网络连接!请手动连接网络", Toast.LENGTH_SHORT).show(); break; case 0: textView.setText("已经接入手机网络,请注意流量!" + type); Toast.makeText(getApplicationContext(), "已经接入手机网络,请注意流量!", Toast.LENGTH_SHORT).show(); break; case 1: textView.setText("WIFI网络" + type); Toast.makeText(getApplicationContext(), "WIFI网络", Toast.LENGTH_SHORT).show(); break; case 2: textView.setText("wap网络" + type); Toast.makeText(getApplicationContext(), "wap网络", Toast.LENGTH_SHORT).show(); break; case 3: textView.setText("net网络" + type); Toast.makeText(getApplicationContext(), "net网络", Toast.LENGTH_SHORT).show(); break; default: break; } } else { textView.setText("无网络接入"); Toast.makeText(getApplicationContext(), "无有网络连接!请手动连接网络", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onStop() { super.onStart(); /** * 卸载广播 */ unregisterReceiver(myReceiver); } /** * 判断有无网络连接 * * @param context * @return */ public boolean isNetworkConnected(Context context) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } return false; }//// public boolean isMobileConnected(Context context) {// if (context != null) {// ConnectivityManager mConnectivityManager = (ConnectivityManager) context// .getSystemService(Context.CONNECTIVITY_SERVICE);// NetworkInfo mMoBileNetworkInfo = mConnectivityManager// .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);// if (mMoBileNetworkInfo != null) {// return mMoBileNetworkInfo.isAvailable();// }// }// return false;// }//// public boolean isWifiConnected(Context context) {// if (context != null) {// ConnectivityManager mConnectivityManager = (ConnectivityManager) context// .getSystemService(Context.CONNECTIVITY_SERVICE);// NetworkInfo mWiFiNetworkInfo = mConnectivityManager// .getNetworkInfo(ConnectivityManager.TYPE_WIFI);// if (mWiFiNetworkInfo != null) {// return mWiFiNetworkInfo.isAvailable();// }// }// return false;// } public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; }}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zdemo.network" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.zdemo.network.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 注册网络判断的广播 start --> <receiver android:name="util.MyReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <!-- 注册网络判断的广播 end --> </application></manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" ><Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击监测网络状态"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_below="@id/btn"/></RelativeLayout>
把代码拷贝运行就OK了。
0 0
- Android网络判断,网络设置
- android 判断网络连接
- Android中判断网络
- network: android 网络判断
- android 判断网络状态
- android 判断网络连接
- android 网络判断
- android 网络判断
- network: android 网络判断
- android 判断网络连接
- network: android 网络判断
- Android判断网络状态
- Android 判断网络状态
- android 网络状态判断
- android之网络判断
- network: android 网络判断
- network: android 网络判断
- android 判断网络连接
- 听说2017你想写前端?
- nginx 配置多个tomcat共用80端口
- k8s容器调度策略
- 堆排序
- 探究为何:在onCreate中通过View.post能获取宽高
- android网络判断
- git ssh传输的原理与实践
- 设计模式深入学习--Proxy 代理模式(结构型模式)
- linux之sed命令详解
- Quartz.NET Windows 服务示例
- 关于android studio debug签名
- STL set集合容器
- 洛谷 P1583 魔法照片
- CI集成支付宝即时到账支付接口