android下检查网络不通的后续合理人性化操作(常用)
2012-11-21 10:45
323 查看
直接上代码 ,注释很详细
Main:
Second:
Common:
Main:
package com.gem.hsx; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main extends Activity { Button btnok; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnok=(Button) findViewById(R.id.btnok); btnok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(Main.this, Second.class); startActivity(intent); } }); } }
Second:
/** * @author hsx */ package com.gem.hsx; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; /** *@author hsx *@date 2012-11-21 *@Description 在android开发中常常遇到网络连接,所以要进行网络判断,本文主要是侧重于在连接网络获取数据之后, *如果网路不通,数据没有获取到之后的操作。本文核心的操作是Common类中的方法 */ public class Second extends Activity { Common common=new Common(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); init(); } /** *@author hsx *@Description * */ /** * 加载起始数据 */ private void init() { /** * 弹出一个进度对话框 */ common.showProgressDialog(Second.this); /** * 在此线程中进行网络连接的操作,并将获取到的数据通过message发送到handler中 */ new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 100000000; i++) { } Message message=new Message(); message.obj=null; handler.sendMessage(message); } }).start(); } Handler handler=new Handler() { /** * 在handler中对数据进行判断,数据是否为空或者是其他,要根据具体情况,进行判断。 */ @Override public void handleMessage(Message msg) { if (msg.obj==null) { common.setNetwork(Second.this); } super.handleMessage(msg); } }; }
Common:
/** * @author hsx */ package com.gem.hsx; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.provider.Settings; /** *@author hsx *@date 2012-11-21 *@Description */ public class Common { private ProgressDialog progressDialog; /** * 显示进度对话框 *@author hsx *@Description *@param context * */ public void showProgressDialog(Context context) { progressDialog = new ProgressDialog(context); progressDialog.setTitle("数据加载中,请等待"); progressDialog.show(); } /** * 网络判断 * @param ctx * @return */ public boolean isNetworkAvailable(Context ctx) { ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); return (info != null && info.isConnected()); } /** * 让进度对话框消失,而且弹出对话框要求用户去设置网络 *@author hsx *@Description *@param context *@param progressDialog * */ public void setNetwork(final Context context) { progressDialog.dismiss(); AlertDialog dialog ; dialog= new AlertDialog.Builder(context).create(); dialog.setTitle("提示"); dialog.setMessage("当前网络不可用,请设置正确的网络"); dialog.setCanceledOnTouchOutside(false); dialog.setButton(DialogInterface.BUTTON_POSITIVE , "设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { context.startActivity(new Intent(Settings.ACTION_SETTINGS)); } }); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } /** * 重写一个方法,主要是可以自定义ProgressDialog *@author hsx *@Description *@param context *@param p * */ public void setNetwork(final Context context,ProgressDialog p) { p.dismiss(); AlertDialog dialog ; dialog= new AlertDialog.Builder(context).create(); dialog.setTitle("提示"); dialog.setMessage("当前网络不可用,请设置正确的网络"); dialog.setCanceledOnTouchOutside(false); dialog.setButton(DialogInterface.BUTTON_POSITIVE , "设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { context.startActivity(new Intent(Settings.ACTION_SETTINGS)); } }); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }
相关文章推荐
- Android 检查当前网络常用例子
- Android 网络操作常用的两个类
- Android 网络操作常用的两个类
- Android开发中4个常用的工具类【Toast、SharedPreferences、网络及屏幕操作】
- 请求对象Android 网络操作常用的两个类
- 【Android Training - Connectivity】基本网络操作 [ Lesson 2 - 管理网络连接的常用方法 ]
- Android中常用的位图操作(View与Bitmap转化、圆角、灰化、提取Alpha、旋转、倒影、剪切……)
- Android的基本常用的短信操作
- Android之检查网络链接情况以及打开网络设置页面
- Android访问网络的常用方式:Handler、AsyncTask简单例子(服务器端+Android端)
- Shell命令之Android及iOS常用操作命令
- android网络操作使用汇总(http)
- Android---网络交互之客户端请求服务端资源后续
- Android的eclipse常用操作详解ZZ
- Android网络常用的请求---HttpURLConnection和HttpClient
- Android网络框架xUtils中的View的视图绑定注解操作
- Android中常用的位图操作(View与Bitmap转化、圆角、灰化、提取Alpha、旋转、倒影、剪切……)
- Android中常用的位图操作
- Android开发-线程和线程消息处理-常用操作
- Android开发-使用SharedPreferences保存数据-常用操作