当前位置: 编程技术>移动开发
本页文章导读:
▪Log的设立 Log的设置
999999999999999999999999
......
▪ 异常使用AsyncTask导致UI处理出错 错误使用AsyncTask导致UI处理出错
■■ 想做的效果是
1.显示Loading画面
2.然后异步在后台访问远程信息
3.返回远程信息后,关闭Loading画面. 如果有错误,显示错误对话框.
.........
▪ 生产序列号独一键值UUID 生产序列号唯一键值UUID
package com.feinar.cbs.common;
import java.util.Random;
import java.util.UUID;
public class GUID {
private static int index = 0;
private static final GUID instance = new GUID();
public static GUID getInstance.........
[1]Log的设立
来源: 互联网 发布时间: 2014-02-18
Log的设置
999999999999999999999999
[2] 异常使用AsyncTask导致UI处理出错
来源: 互联网 发布时间: 2014-02-18
错误使用AsyncTask导致UI处理出错
onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
■■ 想做的效果是
1.显示Loading画面
2.然后异步在后台访问远程信息
3.返回远程信息后,关闭Loading画面. 如果有错误,显示错误对话框.
在实现最初,所有处理都放到了AsyncTask的doInBackground()中,在显示对话框时出异常:
Android Can't create handler inside thread that has not called Looper.prepare()
图得省事,使用了网上说的方法:
Looper.prepare();
显示对话框
Looper.loop();
貌似好用了.
■■ 结果这两天发现画面经常有假死的情况
最终在stackoverflow上找到解决方法
1. doInBackground()
这里面只负责处理数据部分,UI的处理不要放在这里.
2. onPostExecute()
这里才是处理结果的地方,且是运行在UI线程下,不需要做其它处理.
难怪之前使用得怪怪的,我有的地方放在onPostExecute() 里,有的地方就是图省事放到了doInBackground()中,
我还奇怪不是在所有的地方都有这个现象呢.
■■ 刚才查了一下API文档,后悔当初没早点看就开始使用网上的例子,
人家文档写的清楚清楚啊, 555555555555
----------------------------------------------------
[3] 生产序列号独一键值UUID
来源: 互联网 发布时间: 2014-02-18
生产序列号唯一键值UUID
package com.feinar.cbs.common; import java.util.Random; import java.util.UUID; public class GUID { private static int index = 0; private static final GUID instance = new GUID(); public static GUID getInstance() { return instance; } private final static int intMax = 24; private final static char[] ch = { 'A', 'B', 'C', 'E', 'F', 'K', 'M', 'P', 'Q', 'R', 'T', 'U', 'W', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; private int getNum(char schar) { switch (schar) { case 'A': return 1; case 'B': return 2; case 'C': return 3; case 'D': return 4; case 'E': return 5; case 'F': return 6; case 'K': return 7; case 'M': return 8; case 'P': return 9; case 'Q': return 0; case 'R': return 10; case 'T': return 11; case 'U': return 12; case 'W': return 13; case 'Z': return 14; case '1': return 15; case '2': return 16; case '3': return 17; case '4': return 18; case '5': return 19; case '6': return 20; case '7': return 21; case '8': return 22; case '9': return 23; case '0': return 24; default: return 1; } } // 得到前N-1位随机数 private String getRanstr(int intLength) { Random rd = new Random(); rd.setSeed(System.currentTimeMillis()+index); if(index>100){ index=0; }else{ index++; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < intLength; i++) { sb.append(ch[rd.nextInt(intMax)]); } return sb.toString(); } // 得到最后一位数 private String getLast(String strs) { int sum = 0; for (int i = 0; i < strs.length(); i++) { int nums = getNum(strs.charAt(i)); sum += nums * i; } int k = sum % 11; if (k == 0) { return String.valueOf(0); } if (k == 10) { return "X"; } return String.valueOf(k); } // 取得16随机数 public String get16() { String str = getRanstr(15); str = str.concat(getLast(str)); return str; } // 取得12随机数 public String get12() { String strg = getRanstr(11); strg = strg.concat(getLast(strg)); return strg; } public static void main(String[] args) { System.out.println(getInstance().get16()); // UUID uuid = UUID.randomUUID(); // System.out.println(uuid); } }
最新技术文章: