- 浏览: 633969 次
文章分类
最新评论
-
树下白狐:
listview与adapter用法 -
u011467537:
...
Android背景渐变色(shape,gradient) -
asdf12343800:
asdf12343800 写道asdf12343800 写道a ...
Android背景渐变色(shape,gradient) -
asdf12343800:
asdf12343800 写道asdf12343800 写道 ...
Android背景渐变色(shape,gradient) -
asdf12343800:
asdf12343800 写道 : lol: fghhg
Android背景渐变色(shape,gradient)
在发送任何HTTP请求前最好检查下网络连接状态,这样可以避免异常。这个教程将会介绍怎样在你的应用中检测网络连接状态。
创建新的项目
1.在Eclipse IDE中创建一个新的项目并把填入必须的信息。 File->New->Android Project
2.创建新项目后的第一步是要在AndroidManifest.xml文件中添加必要的权限。
- 为了访问网络我们需要 INTERNET 权限
- 为了检查网络状态我们需要 ACCESS_NETWORK_STATE 权限
01 |
<? xml version = "1.0" encoding = "utf-8" ?>
|
02 |
< manifest xmlns:android = "http://schemas.android.com/apk/res/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 = "http://schemas.android.com/apk/res/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(AndroidDetectInternetConnectionActivity. 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 layout查找顺序
2013-01-07 14:11 1142layout可以按后缀支持多分辨率,分辨率layout-mdp ... -
listview与adapter用法
2012-09-11 08:59 97425一个ListView通常有两 ... -
intent 传值
2012-09-11 08:58 1082intent.putExtra()存 getInten ... -
android(java)控制http连接超时的问题
2012-09-10 15:39 1528"声明一个boolean公共变量,表明当前httpc ... -
android 设置http等待超时
2012-09-10 15:39 11605BasicHttpParams httpPar ... -
android 设置椭圆
2012-09-10 15:39 2336<shape xmlns:android=& ... -
Android 自定义Button按钮显示样式(正常、按下、获取焦点)
2012-09-10 15:39 1287首先写一个定义Button样式的XML文件:新建Andr ... -
android 按两次返回键退出
2012-09-07 11:12 5377package com.anjoyo.test; ... -
Android Animation(二)
2012-09-07 10:58 944关于动画的实现,Andr ... -
activity切换动画
2012-09-07 10:55 4181在介绍切换动画效果前,先介绍下将使用到的Android ... -
android 震动
2012-09-07 10:02 2059让手机震动,需要创建Vibrator对象,通过调用vi ... -
android.R.layout下有一些常量存在哪?
2012-09-07 00:35 1156在你的android SDK目录下可以看到 例如 你的 and ... -
Android禁止横屏竖屏切换
2012-09-05 13:43 989在Android中要让一个程序的界面始终保持一个方向,不 ... -
ListView滚动加载(类似Android Market的列表)
2012-09-04 21:08 1758ListView滚动加载(类似Android Mark ... -
android 设置EditText只能输入数字
2012-09-03 23:23 47690android:numeric="intege ... -
填充Listview第一个Item的分隔线
2012-08-31 15:53 63131. 在res/drawable/目录下新建 mygra ... -
android相对父控件比例
2012-08-31 15:39 969shape xml中的gradient能用 70%p 这 ... -
分割线样式
2012-08-31 14:09 1083android给listview设置分割线Divider ... -
Android时钟
2012-08-31 13:21 21301. Android时钟,效果图: 2. ... -
android timer
2012-08-31 09:01 880Timer timer=new Timer; timer.s ...
相关推荐
android 检查网络连接状态实现步骤
获取android手机的连接状态的实例。
android 网络连接 网络状态 用于获取当前使用的网络情况
仿QQ微信,Android开发中网络连接状态的判断(判断android设备是否有网络),详细了解请移步http://blog.csdn.net/zxc514257857/article/details/69219113
判断Android客户端网络连接状态ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean ...
Android获取当前网络状态并检测网络的连接是wifi或是2g/3g
android网络监听变化,判断当前是什么网络类型,实时监听。android网络监听变化,判断当前是什么网络类型,实时监听。android网络监听变化,判断当前是什么网络类型,实时监听。android网络监听变化,判断当前是什么...
Android Socket连接,包含心跳检测,断线重连,数据发送等等 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/78094909
android wifi连接打印机并获取打印机状态 博客地址http://blog.csdn.net/jinfulin/article/details/52622055
安卓Android源码——网络连接状态检测.rar
android判断网络连接状态,以及网络连接类型
Wifi连接网络,通过程序你将了解到定义WifiManager对象,定义WifiInfo对象,获取网络连接列表,定义一个WifiLock,打开Wifi网卡,关闭Wifi网卡,检查当前Wifi网卡状态,扫描周边网络,每次点击扫描之前清空上一次的...
android监听网络连接状态,使用BroadCastReciver 博客地址: http://blog.csdn.net/pangzaifei/article/details/19498577
兼容Android29以下获取网络连接及网络类型的工具。
android判断网络状态、获取网络运营商、网络类型的工具类,并提供打开网络设置界面接口
Android编程获取网络连接状态(3GWifi)及调用网络配置界面
在使用Android连接网络的时候,并不是每次都能连接到网络,在这个时候,我们最好是在程序启动的时候对网络的状态进行一下判断,如果没有网络则进行即时提醒用户进行设置。 要判断网络状态,首先需要有相应的权限,...
android网络监听变化,判断当前是什么网络类型。。。。。。。。。。。。。。。。。
安卓Android源码——网络连接状态检测.zip
Android应用源码之网络连接状态检测.zip