当前位置: 编程技术>移动开发
本页文章导读:
▪单例模式学习及统制实例化对象个数 单例模式学习及控制实例化对象个数
单例模式学习:package com.amaker.single;
import java.util.HashMap;
import java.util.Map;
public class People {
//===============================================================
//标准.........
▪ 上载工具 下载工具
class DownloadAsyncTask extends AsyncTask<String[], Integer, String> {
private String fileName;
private long length;
DownloadAsyncTask(String fileName) {
this.fileName = fileName;
}
.........
▪ Activity栈跟taskAffinity属性 Activity栈和taskAffinity属性
留下两篇文章,回去读读
http://blog.csdn.net/lygglobetech/article/details/6600692
http://blog.sina.com.cn/s/blog_61f4999d0100qlv5.html
......
[1]单例模式学习及统制实例化对象个数
来源: 互联网 发布时间: 2014-02-18
单例模式学习及控制实例化对象个数
单例模式学习:
单例模式学习:
package com.amaker.single; import java.util.HashMap; import java.util.Map; public class People { //=============================================================== //标准的单例 private static People p = null; private People(){ } public static People getInstance(){ if(p==null){ p = new People(); } return p; } //=============================================================== //利用缓存的单例 private static Map<String,People> map = new HashMap<String,People>(); public static People getInstance2(){ People p = map.get("one"); if(p==null){ p = new People(); map.put("one", p); } return p; } //=============================================================== //实例化三个或更多实例,可以按要求实例化对象个数,如下面的count可以从配置文件中读取 private static int number = 1; private static int count = 3; public static People getInstance3(){ p = map.get(number+""); if(p==null){ p = new People(); map.put(number+"", p); } number++; if(number>count){ number = 1; } return p; } }
package com.amaker.client; import com.amaker.single.People; public class Test { public static void main(String[] args) { //单例测试: /*People p1 = People.getInstance(); System.out.println("p=="+p1); People p2 = People.getInstance(); System.out.println("p=="+p2); People p3 = People.getInstance(); System.out.println("p=="+p3); */ //缓存测试: /*People p1 = People.getInstance2(); System.out.println("p=="+p1); People p2 = People.getInstance2(); System.out.println("p=="+p2); People p3 = People.getInstance2(); System.out.println("p=="+p3);*/ //控制对象的个数,暂时实例化三个对象 People p1 = People.getInstance3(); System.out.println("p=="+p1); People p2 = People.getInstance3(); System.out.println("p=="+p2); People p3 = People.getInstance3(); System.out.println("p=="+p3); People p4 = People.getInstance3(); System.out.println("p=="+p4); People p5 = People.getInstance3(); System.out.println("p=="+p5); } }
[2] 上载工具
来源: 互联网 发布时间: 2014-02-18
下载工具
class DownloadAsyncTask extends AsyncTask<String[], Integer, String> { private String fileName; private long length; DownloadAsyncTask(String fileName) { this.fileName = fileName; } @Override protected void onPreExecute() { mProgressLayout.setVisibility(View.VISIBLE); mBtnUpdate.setVisibility(View.GONE); mBtnBack.setVisibility(View.GONE); mProgressBar.setIndeterminate(false); mBtnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DownloadAsyncTask.this.cancel(true); mProgressLayout.setVisibility(View.GONE); mBtnUpdate.setVisibility(View.VISIBLE); mBtnBack.setVisibility(View.VISIBLE); mBtnUpdate.setText(R.string.upgrade_now); } }); super.onPreExecute(); } @Override protected String doInBackground(String[]... params) { InputStream is = null; String[] fileInfos = params[0]; try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(fileInfos[0]); HttpResponse response; response = client.execute(get); HttpEntity entity = response.getEntity(); length = entity.getContentLength(); mProgressBar.setMax((int) (length / 1024)); is = entity.getContent(); FileOutputStream fileOutputStream = null; if (is != null) { File file = new File(fileInfos[1], fileInfos[2]); fileOutputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int ch = -1; int count = 0; while ((ch = is.read(buf)) != -1) { if (isCancelled()) { break; } fileOutputStream.write(buf, 0, ch); count += ch; publishProgress((int) (count / 1024)); } } fileOutputStream.flush(); if (fileOutputStream != null) { fileOutputStream.close(); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return fileInfos[2]; } @Override protected void onProgressUpdate(Integer... values) { String size = FileManager.formartFileSize(length); String downloadSize = FileManager.formartFileSize(values[0] * 1024); mProgressBar.setProgress(values[0]); mProgressPersent.setText(values[0] * 1024 * 100 / length + "%"); mProgressSize.setText(downloadSize + "/" + size); super.onProgressUpdate(values); } @Override protected void onPostExecute(String result) { Message msg = Message.obtain(mHandler, MessageWhat.ACTION_DOWNLOAD_COMPLETED, this.fileName); mHandler.sendMessage(msg); super.onPostExecute(result); } }
[3] Activity栈跟taskAffinity属性
来源: 互联网 发布时间: 2014-02-18
Activity栈和taskAffinity属性
留下两篇文章,回去读读
http://blog.csdn.net/lygglobetech/article/details/6600692
http://blog.sina.com.cn/s/blog_61f4999d0100qlv5.html
最新技术文章: