当前位置:  编程技术>移动开发
本页文章导读:
    ▪单例模式学习及统制实例化对象个数        单例模式学习及控制实例化对象个数 单例模式学习: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


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3