Android(安卓)判斷機子是否聯網和聯網方式
16lz
2022-07-26
判斷網絡是否連接
判斷聯網方式
private boolean CheckNetwork() {boolean flag = false;ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);if (cwjManager.getActiveNetworkInfo() != null)flag = cwjManager.getActiveNetworkInfo().isAvailable();if (!flag) {Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage(getResources().getString("请开启GPRS或WIFI网络连接"));b.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Intent mIntent = new Intent("/");ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");mIntent.setComponent(comp);mIntent.setAction("<span class="hilite">android</span>.intent.action.VIEW");startActivity(mIntent);}}).setNeutralButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {dialog.cancel();}}).create();b.show();}return flag;}
判斷聯網方式
setContentView(R.layout.main); text = (TextView) findViewById(R.id.text); ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); if (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) text.setText("wifi方式连接"); if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED) text.setText("GPRS方式连接");
更多相关文章
- Android使用Retrofit进行网络请求
- android 创建桌面快捷方式 、插件
- android源码下载方式
- Android中判断网络功能是否可用
- Android的网络状态判断
- 创建android逐帧动画的两种方式
- 【安卓笔记】android客户端与服务端交互的三种方式
- Android打开/关闭数据流量
- 重定向android log