Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之獲取所有軟件信息

Android開發之獲取所有軟件信息

編輯:關於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>items=new ArrayList>();
	private SimpleAdapter adapter;
	// 取得所有安裝軟件信息
	private List allPackageInfos;
	// 取得自己安裝的軟件信息
	private List userPackageInfos;
	// 取得系統安裝的軟件信息
	private List 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) 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();
				// 定義系統安裝軟件信息包
				sysPackageInfos = new ArrayList();				
				// 循環取出所有軟件信息
				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);
					Mapitem=new HashMap();
					//獲取程序的包名
					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 sysPackageInfos,List 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();
		}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved