Android开发之获取所有软件信息

程序运行效果图:


程序代码:

/**
 * 获取所有软件信息
 * 1.通过异步的方式显示系统中所有软件
 * 2.单击打开指定软件
 * 3.将所有软件的包名和activity名保存的本地SharedPreferences
 * @author jph
 * Date:2014.09.21
 */
public class ScanPackage1 extends Activity {
	/**扫描成功**/
	private final static int FLAG_LOAD_SUCCESS=0x10001;
	private final static int SCANNING=0x10002;	
	private ListView list;
	private List<Map<String, Object>>items=new ArrayList<Map<String,Object>>();
	private SimpleAdapter adapter;
	// 取得所有安装软件信息
	private List<PackageInfo> allPackageInfos;
	// 取得自己安装的软件信息
	private List<PackageInfo> userPackageInfos;
	// 取得系统安装的软件信息
	private List<PackageInfo> sysPackageInfos;	
	Handler mHandler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case FLAG_LOAD_SUCCESS://完成扫描
				
				break;
			case SCANNING://正在扫描					
				items.add((Map<String, Object>) msg.obj);
				//通知适配器数据改变
				adapter.notifyDataSetChanged();				
				break;

			default:
				break;
			}			
		}			
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sp_layout);
		list=(ListView)findViewById(R.id.list);
		new ScanThread().start();
		adapter=new SimpleAdapter(this, items, R.layout.line, new String[]{
				"imgIco","appName","packageName"
		}, new int[]{R.id.imgIco,R.id.tvAppName,R.id.tvAppDesc});
		list.setAdapter(adapter);
		//ViewBinder该类可以帮助SimpleAdapter加载图片(如:Bitmap,Drawable)
		adapter.setViewBinder(new ViewBinder() {			
			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				// TODO Auto-generated method stub
				 if(view instanceof ImageView  && data instanceof Drawable){    
		              ImageView iv = (ImageView) view;    
		              iv.setImageDrawable((Drawable) data);		               
		               return true;    
		           }else{
		        	   return false;  
		           }   
			}
		});
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				try {
					PackageInfo pInfo=allPackageInfos.get(arg2);
					Intent intent=new Intent();
					intent.setComponent(new ComponentName(pInfo.packageName, pInfo.activities[0].name));				
					startActivity(intent);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				
			}
		});
		
	}
	// ***************--------*创建一个线程加载安装程序*--------------*******************//
		private class ScanThread extends Thread {
			@Override
			public void run() {
				// 取得系统安装所有软件信息
				allPackageInfos = getPackageManager().getInstalledPackages(
						PackageManager.GET_UNINSTALLED_PACKAGES
								| PackageManager.GET_ACTIVITIES);
				// 定义用户安装软件信息包
				userPackageInfos = new ArrayList<PackageInfo>();
				// 定义系统安装软件信息包
				sysPackageInfos = new ArrayList<PackageInfo>();				
				// 循环取出所有软件信息
				for (int i = 0; i < allPackageInfos.size(); i++) {					
					// 得到每个软件信息
					PackageInfo temp = allPackageInfos.get(i);
					ApplicationInfo appInfo = temp.applicationInfo;
					if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0
							|| (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
						//系统软件
						sysPackageInfos.add(temp);						
						
					} else {
						//用户自己安装软件
						userPackageInfos.add(temp);							
					}	
					//获取程序的图标
					Drawable ico=ScanPackage1.this.getPackageManager().getApplicationIcon(appInfo);
					//获取程序的名称
					String appName=(String) ScanPackage1.this.getPackageManager().getApplicationLabel(appInfo);
					Map<String, Object>item=new HashMap<String, Object>();
					//获取程序的包名
					String packageName=appInfo.packageName;			
					item.put("imgIco", ico);
					item.put("appName", appName);
					item.put("packageName", packageName);
					Message message = new Message();
	                message.what =SCANNING;
	                message.obj = item;
	                mHandler.sendMessage(message);
										
				}
				saveInfo(sysPackageInfos, userPackageInfos);			
				mHandler.sendEmptyMessage(FLAG_LOAD_SUCCESS);
			}
		};
		
		/**
		 * 将系统中所装程序的信息写入到配置文件	
		 * @param sysPackageInfos 系统安装软件信息包
		 * @param userPackageInfos 用户安装软件信息包
		 */
		private void saveInfo(List<PackageInfo> sysPackageInfos,List<PackageInfo> userPackageInfos) {	
		   //将用户安装的软件添加到添加到系统软件的集合中
			sysPackageInfos.addAll(userPackageInfos);
			SharedPreferences sp = this.getSharedPreferences("appInfs",
					MODE_PRIVATE);
			Editor editor = sp.edit();
			for (int i = 0; i < sysPackageInfos.size(); i++) {
				try {
					//获取程序的包名
					String packageName = sysPackageInfos.get(i).packageName;
					// 取出activity信息
					ActivityInfo activityInfo = sysPackageInfos.get(i).activities[0];
					// 取出activity名字
					String activityName=activityInfo.name;					
					//将程序的信息写入到配置文件
					editor.putString(packageName, activityName);					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}	
				
			}		
			editor.commit();
		}
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。