如何检查Android网络连接状态如何检查Android网络连接状态
在送任何发发发发HTTP发发发发发发发发发发发发发发发发发发发发发发发发发发发发求前最好下网接状,可以避免异常。个教程将会介
怎在你的用中网接状。发发发发发发发发发发发发发发发发
发发发建新的目
1.在Eclipse IDE发发发发发发发发发发发发发发发发发发 中建一个新的目并把填入必的信息。
File->;New->;Android Project
2.发发发发发建新目后的第一是要在步AndroidManifest.xml发发发文件中添加必要的限。
发发发发发发发发发 了网我需要IN...
如何检查Android网络连接状态
在送任何发发发发HTTP发发发发发发发发发发发发发发发发发发发发发发发发发发发发求前最好下网接状,可以避免异常。个
将会介
怎在你的用中网接状。发发发发发发发发发发发发发发发发
发发发建新的目
1.在Eclipse IDE发发发发发发发发发发发发发发发发发发 中建一个新的目并把填入必的信息。
File->;New->;Android Project
2.发发发发发建新目后的第一是要在步AndroidManifest.xml发发发文件中添加必要的限。
发发发发发发发发发 了网我需要INTERNET 发 限
发发发发发发发发发发发 了网状我需要ACCESS_NETWORK_STATE 发 限
AndroidManifest.xml
01
<;?xml version=";1.0"; encoding=";utf-8";?>;
02
<;manifest xmlns:android=";;;
03
package=";com.example.detectinternetconnection";
04
android:versionCode=";1";
05
android:versionName=";1.0"; >;
06
07
<;uses-sdk android:minSdkVersion=";8"; />;
08
09
<;application
10
android:icon=";@drawable/ic_launcher";
11
android:label=";@string/app_name"; >;
12
<;activity
13
android:name=";.AndroidDetectInternetConnectionActivity";
14
android:label=";@string/app_name"; >;
15
<;intent-filter>;
16
<;action android:name=";android.intent.action.MAIN"; />;
17
18
<;category android:name=";android.intent.category.LAUNCHER"; />;
19
<;/intent-filter>;
20
<;/activity>;
21
<;/application>;
22
23
<;!-- Internet Permissions -->;
24
<;uses-permission android:name=";android.permission.INTERNET"; />;
25
26
<;!-- Network State Permissions -->;
27
<;uses-permission
android:name=";android.permission.ACCESS_NETWORK_STATE"; />;
28
29
<;/manifest>;
3.发发发发发建一个新的,名ConnectionDetector.java发发发发发发发,并入以下代。
ConnectionDetector.java
01
package com.example.detectinternetconnection;
02
03
import android.content.Context;
04
import android.net.ConnectivityManager;
05
import android.net.NetworkInfo;
06
07
public class ConnectionDetector {
08
09
private Context _context;
10
11
public ConnectionDetector(Context context){
12
this._context = context;
13
}
14
15
public boolean isConnectingToInternet(){
16
ConnectivityManager connectivity = (ConnectivityManager)
_context.getSystemService(Context.CONNECTIVITY_SERVICE);
17
if (connectivity != null)
18
{
19
NetworkInfo[] info = connectivity.getAllNetworkInfo();
20
if (info != null)
21
for (int i = 0; i <; info.length; i++)
22
if (info[i].getState() == NetworkInfo.State.CONNECTED)
23
{
24
return true;
25
}
26
27
}
28
return false;
29
}
30
}
4.发发发发发发发发发发发发当你需要在你的用中网状用isConnectingToInternet()函数,它会返回true或
false 。
1
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
2
3
Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false
5.发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发在个教程中了我的放置了一个按。只要按下个按就会出
一个 alert dialog 发发发发发发发示网接状。
6.发 打res/layout 发发发 目下的main.xml 发发发发发发发并建一个按。
main.xml
01
<;?xml version=";1.0"; encoding=";utf-8";?>;
02
<;RelativeLayout
xmlns:android=";;;
03
android:layout_width=";fill_parent";
04
android:layout_height=";fill_parent";
05
android:orientation=";vertical"; >;
06
07
<;TextView
08
android:layout_width=";fill_parent";
09
android:layout_height=";wrap_content";
10
android:text=";Detect Internet Status"; />;
11
12
<;Button android:id=";@+id/btn_check";
13
android:layout_height=";wrap_content";
14
android:layout_width=";wrap_content";
15
android:text=";Check Internet Status";
16
android:layout_centerInParent=";true";/>;
17
18
<;/RelativeLayout>;
7.发发发 最后打你的MainActivity 发发发发发发发发发发发发发发发发发发文件并粘下面的代。在下面的代中我用一个
alert dialog 发发发发发发发发发发 来示期的状信息。
01
package com.example.detectinternetconnection;
02
03
import android.app.Activity;
04
import android.app.AlertDialog;
05
import android.content.Context;
06
import android.content.DialogInterface;
07
import android.os.Bundle;
08
import android.view.View;
09
import android.widget.Button;
10
11
public class AndroidDetectInternetConnectionActivity extends Activity {
12
13
// flag for Internet connection status
14
Boolean isInternetPresent = false;
15
16
// Connection detector class
17
ConnectionDetector cd;
18
19
@Override
20
public void onCreate(Bundle savedInstanceState) {
21
super.onCreate(savedInstanceState);
22
setContentView(R.layout.main);
23
24
Button btnStatus = (Button) findViewById(R.id.btn_check);
25
26
// creating connection detector class instance
27
cd = new ConnectionDetector(getApplicationContext());
28
29
/**
30
* Check Internet status button click event
31
* */
32
btnStatus.setOnClickListener(new View.OnClickListener() {
33
34
@Override
35
public void onClick(View v) {
36
37
// get Internet status
38
isInternetPresent = cd.isConnectingToInternet();
39
40
// check for Internet status
41
if (isInternetPresent) {
42
// Internet Connection is Present
43
// make HTTP requests
44
showAlertDialog(AndroidDetectInternetConnectionActi
vity.this,";Internet Connection";,
45
";You have internet connection";, true);
46
} else {
47
// Internet connection is not present
48
// Ask user to connect to Internet
49
showAlertDialog(AndroidDetectInternetConnectionActivity.this, ";No Internet Connection";,
50
";You don't have internet connection.";, false);
51
}
52
}
53
54
});
55
56
}
57
58
/**
59
* Function to display simple Alert Dialog
60
* @param context - application context
61
* @param title - alert dialog title
62
* @param message - alert message
63
* @param status - success/failure (used to set icon)
64
* */
65
public void showAlertDialog(Context context, String title, String
message, Boolean status) {
66
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
67
68
// Setting Dialog Title
69
alertDialog.setTitle(title);
70
71
// Setting Dialog Message
72
alertDialog.setMessage(message);
73
74
// Setting alert dialog icon
75
alertDialog.setIcon((status) ? R.drawable.success :
R.drawable.fail);
76
77
// Setting OK Button
78
alertDialog.setButton(";OK";, new DialogInterface.OnClickListener() {
79
public void onClick(DialogInterface dialog, int which) {
80
}
81
});
82
83
// Showing Alert Message
84
alertDialog.show();
85
}
86
}
发发发发发发发发发发发发发发发发发发发发发发运行并下你的程序吧:你将会得到似下面的果。
本文档为【如何检查Android网络连接状态】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。