2013年2月7日星期四

android注册 登录+修改帐号密码+添加资料+给指定帐号充值



最近公司让写个android注册 登录 修改帐号密码 添加资料 给新注册用户充值DEMO  现在功能都已经OK  目前只剩下一些小细节  现在我就把源码发布出来 给一些需要的人参考,在这里重点只讲怎么去请求服务器 和服务器返回的一些什么东西给我们 我们如何拿到 如何处理最后的时候我会把整个项目打包

都是本人原创 只是我提前在EOE论坛发布了。

有图有真相:
device-2012-11-05-212526.png


device-2012-11-05-212729.png


device-2012-11-05-212758.png


device-2012-11-05-212923.png


device-2012-11-05-212938.png



device-2012-11-05-212948.png

首先我们先看一下请求服务器的类 如何写的 可结合我上一篇:
http://www.eoeandroid.com/thread-212252-1-1.html   
这里就完善上一篇的内容:


我们首先看注册那边返回的JSON都是什么内容吧(一会我会讲到如何抓到result>1的错误信息,然后返回给activity显示出来)  一般你们公司都会有个后台给你们写个接口文档扔给你 让你去做的

?
[java] view plaincopy
  1. { “result”: 1, “uid”:22771,”msg”:””}  
  2.    
  3. result=1 注册成功, uid为玩家uid, msg为空  
  4.    
  5. result>1时注册失败, 此时返回的 uid=0  
  6. result=2:  msg:用户名格式不对   
  7. result=3:  msg:此用户名已经被注册  
  8. result=4:  msg:密码格式不对(长度不是6-16位或者包含了其他字符,比如中文标点之类)  
  9. result=5:  msg:广告来源为空 from 的值不允许为空   
  10. result=6:  msg:系统维护,此时不允许注册  
  11. result>6 时为其他错误, msg会返回错误的具体原因  



我们在注册页面去请求服务器:

在我的工程:Register类


?
[java] view plaincopy
  1. boolean flag ;//条件成立跳转到登陆界面  
  2.     /** 请求服务器 */  
  3.         if (userName != null || password != null || from != null) {  
  4.             flag = UserDataServiceHelper.Register(context, "reg", userName, password, from);  
  5.             if(flag){  
  6.                 Intent intent = new Intent();  
  7.                 intent.putExtra("name", userName);  
  8.                 intent.putExtra("pw", password);  
  9.                 intent.putExtra("fm",from);  
  10.                 intent.setClass(Register.this, Login.class);  
  11.                 startActivity(intent);  
  12.             }else {  
  13.                 Log.i("TAG""不成立");  
  14.             }  
  15.             Log.i("TAG""请求服务器" + userName + password + from);  
  16.         }  



传的参数里面 第一个就是context  第二个就是一个注册的参数(你们后台都会有自己弄一个参数来区分的),第三个参数就是你的名字 第4个参数就是你的密码 第五个参数其实就是一个渠道的意思(从那个渠道过来注册的,比如你从googlePlay注册的 这里随便定义一个参数 让你们的老大知道这个从googlePlay下载注册的,现在产品都这样搞的)
如果注册接口成功返回true 那么flag就会是true(默认是false嘛)  就去执行Intent,然后putExtra 把需要的东西传值到登录界面

登录界面会做什么事呢? 接着上面的问题 看下面的代码
?
[java] view plaincopy
  1. /** 初始化注册VIEW组件 */  
  2.   private void findViewById() {  
  3.       view_userName = (EditText) findViewById(R.id.loginUserNameEdit);  
  4.       view_password = (EditText) findViewById(R.id.loginPasswordEdit);  
  5.       view_rememberMe = (CheckBox) findViewById(R.id.loginRememberMeCheckBox);  
  6.       view_loginSubmit = (Button) findViewById(R.id.loginSubmit);  
  7.       view_loginRegister = (Button) findViewById(R.id.loginRegister);  
  8.       view_fast = (TextView) findViewById(R.id.fast);  
  9.   
  10.       /** 注册成功后传过来用户名和密码,显示在登录界面 */  
  11.       if (!flag) {  
  12.           Intent intent = getIntent();  
  13.           userName = intent.getStringExtra("name");  
  14.           password = intent.getStringExtra("pw");  
  15.           from = intent.getStringExtra("fm");  
  16.           view_rememberMe.setChecked(false);//小BUG  
  17.           view_userName.setText(userName);  
  18.           view_password.setText(password);  
  19.       }  
  20.   
  21.   }  



登录界面就会用getStringExtra方法把刚刚从注册传过来的值  这里是根据Key,value  我们只要得到这个key("name")就OK了。  
然后我们在用setText 显示在editText上!
?
[java] view plaincopy
  1. view_userName.setText(userName);  
  2.             view_password.setText(password);  





然后我们在点击登录的时候看代码 如何把刚刚从注册传过来的值在去传到登录接口 其实在这里很简单 在赋值给的string就OK了 赋值好后然后在把赋值的值传到登录接口 看例子:
?
[java] view plaincopy
  1. /** 登录Button Listener */  
  2.   private OnClickListener submitListener = new OnClickListener() {  
  3.   
  4.       @Override  
  5.       public void onClick(View v) {  
  6.           Log.i("TAG""submitListener");  
  7.           proDialog = ProgressDialog.show(Login.this"连接中..",  
  8.                   "连接中..请稍后...."truetrue);  
  9.           // 开启一个线程进行登录验证,主要是用户失败成功可以直接通过startAcitivity(Intent)转向  
  10.           Thread loginThread = new Thread(new LoginFailureHandler());  
  11.           loginThread.start();// 开启  
  12.   
  13.       }  
  14.   };  



?
[java] view plaincopy
  1. // 另起一个线程登录  
  2.     class LoginFailureHandler implements Runnable {  
  3.         @Override  
  4.         public void run() {  
  5.             userName = view_userName.getText().toString();  
  6.             Log.i("TAG""userName LoginFailureHandler" + userName);  
  7.             password = view_password.getText().toString();  
  8.             Log.i("TAG""password LoginFailureHandler" + password);  
  9.    
  10.             /** 请求服务器 */  
  11.             if (userName != null || password != null) {  
  12.                 boolean loginState = UserDataServiceHelper.logins(context, "login", userName,  
  13.                         password, from);  
  14.    
  15.                 Log.i("TAG""登录返回条件" + loginState);  
  16.                 // 登录成功  
  17.                 if (loginState) {  
  18.                     String LoginUerId = UserDataService.LoginUid;  
  19.                     // 需要传输数据到登陆后的界面,  
  20.                     Intent intent = new Intent();  
  21.                     intent.setClass(Login.this, IndexPage.class);  
  22.                     Bundle bundle = new Bundle();  
  23.                     bundle.putString("LOGIN_USERNAME", userName);  
  24.                     bundle.putString("LOGIN_PASSWORD", password);  
  25.                     bundle.putString("LOGIN_ID", LoginUerId);  
  26.                     intent.putExtras(bundle);  
  27.                     // 转向登陆后的页面  
  28.                     startActivity(intent);  
  29.                     // /** 得到请求服务器返回码 */  
  30.                     String loginStateInt = UserDataService.results;  
  31.                     int Less = Integer.valueOf(loginStateInt); // 转换成整形  
  32.                     Log.i("TAG""登录后的返回码:" + Less);  
  33.                     if (Less == 1) {  
  34.                         StatusCode = true;  
  35.                            
  36.                     }  
  37.    
  38.                     // 登录成功记住帐号密码  
  39.                     if (StatusCode) {  
  40.                         if (isRememberMe()) {  
  41.                             saveSharePreferences(truetrue);  
  42.                         } else {  
  43.                             saveSharePreferences(truefalse);  
  44.                         }  
  45.    
  46.                     } else {  
  47.                         // 如果不是网络错误  
  48.                         if (!isNetError) {  
  49.                             clearSharePassword();  
  50.                             clearShareName();  
  51.    
  52.                         }  
  53.    
  54.                     }  
  55.                     if (!view_rememberMe.isChecked()) {  
  56.                         clearSharePassword();  
  57.                         clearShareName();  
  58.                     }  
  59.                     proDialog.dismiss();  
  60.                 } else {  
  61.                     // 通过调用handler来通知UI主线程更新UI,  
  62.                     Log.i("TAG""连接失败");  
  63.                     Message message = new Message();  
  64.                     Bundle bundle = new Bundle();  
  65.                     bundle.putBoolean("isNetError", isNetError);  
  66.                     message.setData(bundle);  
  67.                     loginHandler.sendMessage(message);  
  68.                 }  
  69.    
  70.             }  
  71.    
  72.         }  
  73.     }  


登录后 我们就会看到一个登录成功的页面,下面有4个按钮 修改帐号,添加资料,从设密码

那么修改帐号的流程 还是跟上面登录注册一样  把所有的值用putExtra方法传过来  然后在修改帐号页面getStringExtra在得到("name",passwor)等  ,在从新声明一个string类型  在赋值  在去请求  修改帐号密码接口

添加资料也就是去请求地址 然后里面需要传什么东西 就传什么过去。

从设置也是和上面的流程 putExtra-----getStringExtra("name",passwor)等在从新声明一个string类型  在赋值  在去请求 
其实注册 登录  修改帐号密码  和添加资料 都是很简单的  无非就是putExtra   getStringExtra  声明   赋值   请求  返回信息   显示
备注:
附件里面我把接口那部分已经删掉了   你们可以看上面我写的请求服务器的类  根据自己的业务需求来添加和删除   请求服务器类写的一个不好的地方就是 每个方法都加了static  呵呵   这个原因主要是  我写了一个方法  然后剩下的接口我就复制上面的 然后改改。



[java] view plaincopy
  1. public class UserDataServiceHelper {  
  2.     /** 时间戳 */  
  3.     static long serial = new Date().getTime();  
  4.    
  5.     /** KEY */  
  6.     static String key = "后台人员会给你个密钥";  
  7.    
  8.     /** 网站地址 */  
  9.     private static final String HOST_IP = "你的请求地址";  
  10.    
  11.     /** 请求服务器 S1,S2,S3等 */  
  12.     static String server = "s1";  
  13.    
  14.     /** 返回码 1=成功 */  
  15.     public static String results = null;  
  16.    
  17.     /** 登录UID */  
  18.     public static String LoginUid = null;  
  19.    
  20.     /** 注册UID */  
  21.     public static String RegisterUid = null;  
  22.    
  23.     /** 快速注册根据返回得到用户名 */  
  24.     public static String FastUserName = null;  
  25.    
  26.     /** 快速注册根据返回得到密码 */  
  27.     public static String FastPassWord = null;  
  28.    
  29.     /** 快速注册根据返回UID */  
  30.     public static String Uid = null;  
  31.        
  32.        
  33.     /** 充值请求地址*/  
  34.     private  static final String HOST_IP_CREDIT = "你的充值请求地址";  
  35.        
  36.    
  37.     // 注册  
  38.     public static boolean Register(Context context, String action, String username,  
  39.             String password, String from) {  
  40.         Log.i("TAG""得到的" + context + action + username + password + from);  
  41.         try {  
  42.             HttpClient httpClient = new DefaultHttpClient();  
  43.             // 请求超时  
  44.             httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);  
  45.    
  46.             // 读取超时  
  47.             httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);  
  48.             StringBuilder uri = new StringBuilder();  
  49.             uri.append(HOST_IP + "XXXXXX" + "?" + "action=" + action);  
  50.             uri.append("&username=");  
  51.             uri.append(username);  
  52.             uri.append("&password=");  
  53.             uri.append(password);  
  54.             uri.append("&from=");  
  55.             uri.append(from);  
  56.             uri.append("&server=");  
  57.             uri.append(server);  
  58.             uri.append("&apitime=");  
  59.             uri.append(serial);  
  60.             uri.append("&key=");  
  61.             uri.append(getMD5Str(serial + key));  
  62.             Log.i("TAG""请求地址:" + uri.toString());  
  63.             HttpPost httpPost = new HttpPost(uri.toString());  
  64.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  65.             String jsonforString = null;  
  66.             String result = null;  
  67.             String result2 = null;  
  68.             String result3 = null;  
  69.             String result4 = null;  
  70.             String result5 = null;  
  71.             String result6 = null;  
  72.             String result7 = null;  
  73.             // 返回json报文  
  74.             if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  75.                 jsonforString = EntityUtils.toString(httpResponse.getEntity());  
  76.                 JSONObject jsonObject = new JSONObject(jsonforString);  
  77.                 Log.i("TAG""JSONObject对象=" + jsonforString);  
  78.                 result = jsonObject.getString("result");  
  79.                 result2 = jsonObject.getString("msg");  
  80.                 Log.i("TAG""result2:返回的错误信息"+result2);  
  81.                 Log.i("TAG""成功还是失败返回" + result);  
  82.                 if (httpClient != null) {  
  83.                     httpClient.getConnectionManager().shutdown();  
  84.                 }  
  85.                 if (result.equalsIgnoreCase("1")) {  
  86.                     return true;  
  87.                 } else {  
  88.                     return false;  
  89.                 }  
  90.                    
  91.                    
  92.                    
  93.                    
  94.                    
  95.             }  
  96.         } catch (ConnectException e) {  
  97.             Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();  
  98.         } catch (SocketTimeoutException e) {  
  99.             Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();  
  100.    
  101.         } catch (Exception e) {  
  102.             e.printStackTrace();  
  103.         }  
  104.         return false;  
  105.     }  
  106.    
  107.     // 登录  
  108.     public static boolean logins(Context context, String action, String username, String password,  
  109.             String from) {  
  110.         Log.i("TAG""得到的" + context + action + username + password);  
  111.         try {  
  112.             HttpClient httpClient = new DefaultHttpClient();  
  113.             // 请求超时  
  114.             httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);  
  115.    
  116.             // 读取超时  
  117.             httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);  
  118.             StringBuilder uri = new StringBuilder();  
  119.             uri.append(HOST_IP + "XXXXXXXX" + "?" + "action=" + action);  
  120.             uri.append("&username=");  
  121.             uri.append(username);  
  122.             uri.append("&password=");  
  123.             uri.append(password);  
  124.             uri.append("&from=");  
  125.             uri.append(from);  
  126.             uri.append("&server=");  
  127.             uri.append(server);  
  128.             uri.append("&apitime=");  
  129.             uri.append(serial);  
  130.             uri.append("&key=");  
  131.             uri.append(getMD5Str(serial + key));  
  132.             Log.i("TAG""请求地址:" + uri.toString());  
  133.             HttpPost httpPost = new HttpPost(uri.toString());  
  134.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  135.             String jsonforString = null;  
  136.             String result = null;  
  137.             String login = null;  
  138.             // 返回json报文  
  139.             if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  140.                 jsonforString = EntityUtils.toString(httpResponse.getEntity());  
  141.                 JSONObject jsonObject = new JSONObject(jsonforString);  
  142.                 Log.i("TAG""JSONObject对象=" + jsonforString);  
  143.                 result = jsonObject.getString("result");  
  144.                 login = jsonObject.getString("uid");  
  145.                 Log.i("TAG""成功还是失败返回" + result);  
  146.                 if (httpClient != null) {  
  147.                     httpClient.getConnectionManager().shutdown();  
  148.                 }  
  149.                 if (result.equalsIgnoreCase("1")) {  
  150.                     Log.i("TAG""服务器返回码" + result);  
  151.                     results = result;  
  152.                     LoginUid = login;  
  153.                     return true;  
  154.                 } else {  
  155.                     return false;  
  156.                 }  
  157.             }  
  158.         } catch (ConnectException e) {  
  159.             Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();  
  160.         } catch (SocketTimeoutException e) {  
  161.             Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();  
  162.    
  163.         } catch (Exception e) {  
  164.             e.printStackTrace();  
  165.         }  
  166.         return false;  
  167.     }  
  168.    
  169.     // 快速注册  
  170.     public static boolean fast(Context context, String action, String from) {  
  171.         try {  
  172.             HttpClient httpClient = new DefaultHttpClient();  
  173.             // 请求超时  
  174.             httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);  
  175.    
  176.             // 读取超时  
  177.             httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);  
  178.             StringBuilder uri = new StringBuilder();  
  179.             uri.append(HOST_IP + "XXXXXXXX" + "?" + "action=" + action);  
  180.             uri.append("&from=");  
  181.             uri.append(from);  
  182.             uri.append("&server=");  
  183.             uri.append(server);  
  184.             uri.append("&apitime=");  
  185.             uri.append(serial);  
  186.             uri.append("&key=");  
  187.             uri.append(getMD5Str(serial + key));  
  188.             Log.i("TAG""快速注册请求地址:" + uri.toString());  
  189.             HttpPost httpPost = new HttpPost(uri.toString());  
  190.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  191.             String jsonforString = null;  
  192.             String result = null;  
  193.             String UserNames = null;  
  194.             String PassWords = null;  
  195.             String UserId = null;  
  196.             // 返回json报文  
  197.             if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  198.                 jsonforString = EntityUtils.toString(httpResponse.getEntity());  
  199.                 JSONObject jsonObject = new JSONObject(jsonforString);  
  200.                 Log.i("TAG""JSONObject对象=" + jsonforString);  
  201.                 result = jsonObject.getString("result");  
  202.                 Log.i("TAG""快速注册成功还是失败返回码" + result);  
  203.                 UserNames = jsonObject.getString("username");  
  204.                 PassWords = jsonObject.getString("password");  
  205.                 Log.i("TAG""快速注册成功返回的密码" + PassWords);  
  206.                 Log.i("TAG""快速注册成功返回用户名" + UserNames);  
  207.                 UserId = jsonObject.getString("uid");  
  208.                 if (httpClient != null) {  
  209.                     httpClient.getConnectionManager().shutdown();  
  210.                 }  
  211.                 if (result.equalsIgnoreCase("1")) {  
  212.                     Log.i("TAG""快速注册服务器返回码" + result);  
  213.                     results = result;  
  214.                     FastUserName = UserNames;  
  215.                     FastPassWord = PassWords;  
  216.                     Uid = UserId;  
  217.                     return true;  
  218.                 } else {  
  219.                     return false;  
  220.                 }  
  221.             }  
  222.         } catch (ConnectException e) {  
  223.             Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();  
  224.         } catch (SocketTimeoutException e) {  
  225.             Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();  
  226.    
  227.         } catch (Exception e) {  
  228.             e.printStackTrace();  
  229.         }  
  230.         return false;  
  231.     }  
  232.    
  233.     // 修改帐号  
  234.     public static boolean updateData(Context context, String action, String uid, String username,  
  235.             String password, String newname, String newpass) {  
  236.         try {  
  237.             HttpClient httpClient = new DefaultHttpClient();  
  238.             // 请求超时  
  239.             httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);  
  240.    
  241.             // 读取超时  
  242.             httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);  
  243.             StringBuilder uri = new StringBuilder();  
  244.             uri.append(HOST_IP + "XXXXXXXXX" + "?" + "action=" + action);  
  245.             uri.append("&uid=");  
  246.             uri.append(uid);  
  247.             uri.append("&username=");  
  248.             uri.append(username);  
  249.             uri.append("&password=");  
  250.             uri.append(password);  
  251.             uri.append("&newname=");  
  252.             uri.append(newname);  
  253.             uri.append("&newpass=");  
  254.             uri.append(newpass);  
  255.             uri.append("&server=");  
  256.             uri.append(server);  
  257.             uri.append("&apitime=");  
  258.             uri.append(serial);  
  259.             uri.append("&key=");  
  260.             uri.append(getMD5Str(serial + key));  
  261.             Log.i("TAG""提交更新:" + uri.toString());  
  262.             HttpPost httpPost = new HttpPost(uri.toString());  
  263.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  264.             String jsonforString = null;  
  265.             String result = null;  
  266.             String UserNames = null;  
  267.             String PassWords = null;  
  268.             String UserId = null;  
  269.             // 返回json报文  
  270.             if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  271.                 jsonforString = EntityUtils.toString(httpResponse.getEntity());  
  272.                 JSONObject jsonObject = new JSONObject(jsonforString);  
  273.                 // Log.i("TAG", "JSONObject对象=" + jsonforString);  
  274.                 result = jsonObject.getString("result");  
  275.                 Log.i("TAG""提交更新返回码" + result);  
  276.                 // UserNames = jsonObject.getString("username");  
  277.                 // PassWords = jsonObject.getString("password");  
  278.                 // Log.i("TAG", "快速注册成功返回的密码" + PassWords);  
  279.                 // Log.i("TAG", "快速注册成功返回用户名" + UserNames);  
  280.                 UserId = jsonObject.getString("uid");  
  281.                 if (httpClient != null) {  
  282.                     httpClient.getConnectionManager().shutdown();  
  283.                 }  
  284.                 if (result.equalsIgnoreCase("1")) {  
  285.                     Log.i("TAG""提交更新" + result);  
  286.                     // results = result;  
  287.                     // FastUserName = UserNames;  
  288.                     // FastPassWord = PassWords;  
  289.                     // Uid = UserId;  
  290.                     return true;  
  291.                 } else {  
  292.                     return false;  
  293.                 }  
  294.             }  
  295.         } catch (ConnectException e) {  
  296.             Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();  
  297.         } catch (SocketTimeoutException e) {  
  298.             Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();  
  299.    
  300.         } catch (Exception e) {  
  301.             e.printStackTrace();  
  302.         }  
  303.         return false;  
  304.     }  
  305.    
  306.        
  307.    
  308.     // MD5  
  309.     public static String getMD5Str(String str) {  
  310.         MessageDigest messageDigest = null;  
  311.         try {  
  312.             messageDigest = MessageDigest.getInstance("MD5");  
  313.             messageDigest.reset();  
  314.             messageDigest.update(str.getBytes("UTF-8"));  
  315.         } catch (NoSuchAlgorithmException e) {  
  316.             e.printStackTrace();  
  317.    
  318.         } catch (UnsupportedEncodingException e) {  
  319.             e.printStackTrace();  
  320.         }  
  321.    
  322.         byte[] byteArray = messageDigest.digest();  
  323.         StringBuffer md5StrBuff = new StringBuffer();  
  324.         for (int i = 0; i < byteArray.length; i++) {  
  325.             if (Integer.toHexString(0xFF & byteArray<i>).length() == 1) {  
  326.                 md5StrBuff.append("0").append(  
  327.                         Integer.toHexString(0xFF & byteArray<i>));  
  328.             } else {  
  329.                 md5StrBuff.append(Integer.toHexString(0xFF & byteArray<i>));  
  330.             }  
  331.         }  
  332.         return md5StrBuff.toString();  
  333.     }  
  334.    
  335.    
  336. }  
  337. </i></i></i>  

项目下载地址
http://download.csdn.net/detail/zxciop110/4737112

Android全屏Activity的几种方式


方法1. AndroidManifest.xml文件里<activity>里面加上



android:theme="@android:style/Theme.NoTitleBar.Fullscreen"


方法2. 在你的Activity onCreate方法里调用

public void setFullScreen()
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

Android透明无边框圆形进度条之最简单实现


 很多人在项目中做长时间操作时,比如访问web service后台数据,都想显示一个透明无边框的圆形进度条,如下图:
        不幸的是,Android系统自带的ProgressDialog,无论如何设置Theme、style,或者用java代码设置什么属性,边框都是去不掉的,至少我现在还不知道怎么去掉:
        怎么办?
        其实很简单,自定义一个ProgressDialog,加载自己的layout!
        先上layout xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
3 &nbsp;&nbsp; &nbsp;android:layout_width="wrap_content"
4 &nbsp;&nbsp; &nbsp;android:layout_height="wrap_content"
5 &nbsp;&nbsp;&nbsp; android:interpolator="@android:anim/linear_interpolator" />
android:interpolator="@android:anim/linear_interpolator"这句话表示进度条动画是匀速的。

        再来定义一个CustomProgressDialog:
复制代码
 1 public class CustomProgressDialog extends ProgressDialog{
 2 
 3     public CustomProgressDialog(Context context) {
 4         super(context);
 5     }
 6     
 7     public CustomProgressDialog(Context context, int theme) {
 8         super(context, theme);
 9     }
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.dialog_progress);
15     }
16     
17     public static CustomProgressDialog show(Context ctx){
18         CustomProgressDialog d = new CustomProgressDialog(ctx);
19         d.show();
20         return d;
21     }
22 }
复制代码
至于怎么用这个Dialog,就不用我教了吧。new一个CustomProgressDialog实例,然后调用继承来的show()方法也行,直接使用我们上面定义的静态show()方法也可,看个人偏好。
        有几点需要说明一下:
        1. 也可以继承AlertDialog,效果一样。
        2. 虽然我们在onCreate()方法里已经加载了自己的layout,但并不妨碍调用基类的方法,设置title、content,效果和直接用基类ProgressDialog一样,无视自定义。
        easy?本文只是提供一个最简单实现,相信已经能满足大部分的需求了,更深入的东西自己摸索去吧。

android 中查看当前是否联网





方法如下:

ConnectivityManager cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cwjManager.getActiveNetworkInfo();
  if (info != null && info.isAvailable()){
       //do something
       //能联网
        return true;
  }else{
       //do something
       //不能联网
        return false;
  }
如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,具体的可以通过 ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式。

同时要在manifest里面加个权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

文档如下:
boolean android.net.NetworkInfo.isAvailable()

public boolean isAvailable ()
Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include

The device is out of the coverage area for any network of this type.
The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled.
The device's radio is turned off, e.g., because airplane mode is enabled.

Returns
true if the network is available, false otherwise

Android手机、E-mail、QQ验证



   /**
     * 验证字符串是否是email
     * @param str
     * @return
     */
    public static boolean isEmail(String str) {
Pattern pattern = Pattern.compile("[//w//.//-]+@([//w//-]+//.)+[//w//-]+",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
return true;
}else {
return false;
}
}  

    /**
     * 验证是否是手机号码
     * @param str
     * @return
     */
    public static boolean isCellphone(String str) {
Pattern pattern = Pattern.compile("1[0-9]{10}");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
return true;
}else {
return false;
}
}

    /**
     * 验证是否是QQ号码
     * @param str
     * @return
     */
    public static boolean isQQ(String str) {
Pattern pattern = Pattern.compile("[1-9]{5,10}");
Matcher matcher = pattern.matcher(str);
if (matcher.matches()) {
return true;
}else {
return false;
}
}
分享到:
上一篇