android 判断网络是否可用
今天也是沒(méi)有什么好寫(xiě)的,但是自己的強(qiáng)迫癥似乎又犯了,覺(jué)得每天寫(xiě)博客的習(xí)慣不能改,所以在網(wǎng)上搜索了資料,寫(xiě)了這些內(nèi)容。
一。檢查網(wǎng)絡(luò)是否可用,并且彈出提示框,用戶可以去設(shè)置網(wǎng)絡(luò)。
package com.example.network;import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo;public class NetworkUtil {/*** 檢查網(wǎng)絡(luò)是否可用* * @param context* @return*/public static boolean isNetworkAvailable(Context context) {ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);if (manager == null) {return false;}NetworkInfo networkinfo = manager.getActiveNetworkInfo();if (networkinfo == null || !networkinfo.isAvailable()) {return false;}return true;}}
這個(gè)主要通過(guò)手機(jī)的ConnectivityManager來(lái)獲取當(dāng)前的網(wǎng)絡(luò)信息,然后判斷是否有網(wǎng)絡(luò)連接。
當(dāng)網(wǎng)絡(luò)不可用的時(shí)候,我們應(yīng)該提供一個(gè) 界面讓用戶區(qū)設(shè)置網(wǎng)絡(luò)連接。Android 2.3以上跟Android 2.3以前的設(shè)置方式是不一樣的,我們需要判斷一下。
package com.example.network;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
?@Override
?protected void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.activity_main);
?}
?@Override
?protected void onStart() {
??Log.i("MainActivity", "onStart");
??if (!NetworkUtil.isNetworkAvailable(this)) {
???showSetNetworkUI(this);
??} else {
???Toast.makeText(this, "網(wǎng)絡(luò)可用...", 0).show();
??}
??super.onStart();
?}
?@Override
?protected void onResume() {
??Log.i("MainActivity", "onStart");
??super.onResume();
?}
?/*
? * 打開(kāi)設(shè)置網(wǎng)絡(luò)界面
? */
?public void showSetNetworkUI(final Context context) {
??// 提示對(duì)話框
??AlertDialog.Builder builder = new Builder(context);
??builder.setTitle("網(wǎng)絡(luò)設(shè)置提示")
????.setMessage("網(wǎng)絡(luò)連接不可用,是否進(jìn)行設(shè)置?")
????.setPositiveButton("設(shè)置", new DialogInterface.OnClickListener() {
?????@Override
?????public void onClick(DialogInterface dialog, int which) {
??????// TODO Auto-generated method stub
??????Intent intent = null;
??????// 判斷手機(jī)系統(tǒng)的版本 即API大于10 就是3.0或以上版本
??????if (android.os.Build.VERSION.SDK_INT > 10) {
???????intent = new Intent(
?????????android.provider.Settings.ACTION_WIFI_SETTINGS);
??????} else {
???????intent = new Intent();
???????ComponentName component = new ComponentName(
?????????"com.android.settings",
?????????"com.android.settings.WirelessSettings");
???????intent.setComponent(component);
???????intent.setAction("android.intent.action.VIEW");
??????}
??????context.startActivity(intent);
?????}
????})
????.setNegativeButton("取消", new DialogInterface.OnClickListener() {
?????@Override
?????public void onClick(DialogInterface dialog, int which) {
??????dialog.dismiss();
?????}
????}).show();
?}
?@Override
?public boolean onCreateOptionsMenu(Menu menu) {
??// Inflate the menu; this adds items to the action bar if it is present.
??getMenuInflater().inflate(R.menu.main, menu);
??return true;
?}
}
這里打當(dāng)沒(méi)有網(wǎng)絡(luò)時(shí),彈出設(shè)置網(wǎng)絡(luò)對(duì)話框,用戶點(diǎn)擊確定時(shí),可以直接跳到設(shè)置網(wǎng)絡(luò)界面。
后面我還找了判斷網(wǎng)絡(luò)類型的代碼,也很簡(jiǎn)單。
還有一個(gè)判斷網(wǎng)絡(luò)狀態(tài)改變的代碼,就是一個(gè)廣播接收器。
package cn.hackcoder.beautyreader.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.util.Log; import android.widget.Toast; import cn.hackcoder.beautyreader.activity.base.BaseActivity; import cn.hackcoder.beautyreader.utils.NetWorkUtils; public class NetWorkStatusReceiver extends BroadcastReceiver { ??public NetWorkStatusReceiver() { ??} ??@Override ??public void onReceive(Context context, Intent intent) { ????String action = intent.getAction(); ????if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ??????Toast.makeText(context, "network changed", Toast.LENGTH_LONG).show(); ??????BaseActivity.isNetWorkConnected = NetWorkUtils.getAPNType(context)>0; ????} ??} }需要在manifest里注冊(cè)廣播。
<receiver ???android:name=".broadcast.NetWorkStatusReceiver" ???android:enabled="true" ???android:exported="true"> ???<intent-filter> ?????<action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> ???</intent-filter> ?</receiver>
這些是我在網(wǎng)上搜索資料整理得來(lái)的,大家可以自己在網(wǎng)上搜素資料,其實(shí)很簡(jiǎn)單,網(wǎng)上資料也很多。
android 判斷網(wǎng)絡(luò)是否可用就講完了。
就這么簡(jiǎn)單。
總結(jié)
以上是生活随笔為你收集整理的android 判断网络是否可用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android 置Activity全屏和
- 下一篇: android activity切换动画