Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 一起學android之如何獲取手機程序列表以及程序相關信息並啟動指定程序

一起學android之如何獲取手機程序列表以及程序相關信息並啟動指定程序

編輯:關於Android編程

效果圖:

程序列表:

\

啟動程序,獲取程序信息:

\

 

 

\

 

 

代碼如下:

創建一個AppInfo類來表示應用程序

 


public class AppInfo { public CharSequence title;// 程序名 public CharSequence packageName; // 程序包名 Intent intent;// 啟動Intent public Drawable icon;// 程序圖標 /* * 設置啟動該程序的Intent */ final void setActivity(ComponentName className, int launchFlags) { intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setComponent(className); intent.setFlags(launchFlags); } }


 


 

創建程序列表的適配器:

 

/**
 * 程序列表適配器
 * @author bill
 *
 */
public class ShowAppListAdapter extends BaseAdapter {
	private ArrayList appList;
	private LayoutInflater inflater;
	
	public ShowAppListAdapter(Context context,ArrayList appList,
			PackageManager pm) {
		this.appList = appList;
		inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return appList.size();
	}


	public Object getItem(int position) {
		return appList.get(position);
	}


	public long getItemId(int position) {
		return position;
	}


	public View getView(int position, View convertView, ViewGroup parent) {
		final AppInfo info = appList.get(position);
		ViewHolder holder = null;
		if(null == convertView){
			convertView = inflater.inflate(R.layout.app_list_item, null);
			holder = new ViewHolder();
			holder.lv_image = (ImageView) convertView.findViewById(R.id.lv_icon);
			holder.lv_name = (TextView) convertView.findViewById(R.id.lv_item_appname);
			holder.lv_packname = (TextView) convertView.findViewById(R.id.lv_item_packageame);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.lv_image.setImageDrawable(info.icon);
		final CharSequence name = info.title;
		final CharSequence packName = info.packageName;
		holder.lv_name.setText(name);
		holder.lv_packname.setText(packName);
		return convertView;
	}
	private final static  class ViewHolder{
		ImageView lv_image;
		 TextView lv_name;
		 TextView lv_packname;
	}

	
}


 

 

 

 

public class MainActivity extends Activity {
	/*
	 * 應用程序集合
	 */
	private ArrayList appInfos;
	private ListView lv_app;
	/*
	 * 管理應用程序包,並通過它獲取程序信息
	 */
	private PackageManager pm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_list);
		pm = getPackageManager();
		initView();
		new Thread(runable).start();
	}

	private void initView(){
		lv_app = (ListView) findViewById(R.id.app_list_view);
		lv_app.setOnItemClickListener(new AppDetailLinster());
	}

	
	private final Runnable runable = new Runnable() {

		public void run() {
			loadApplications();
			myHandler.obtainMessage().sendToTarget();
		}

	};

	private Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			lv_app.setAdapter(new ShowAppListAdapter(MainActivity.this,
					appInfos, pm));
			
			
		}

	};
	
	/**
	 * 加載應用列表
	 */
	private void loadApplications() {
		PackageManager manager = this.getPackageManager();
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		final List apps = manager.queryIntentActivities(
				mainIntent, 0);
		Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
		if (apps != null) {
			final int count = apps.size();
			if (appInfos == null) {
				appInfos = new ArrayList(count);
			}
			appInfos.clear();
			for (int i = 0; i < count; i++) {
				AppInfo application = new AppInfo();
				ResolveInfo info = apps.get(i);
				application.title = info.loadLabel(manager);
				application.setActivity(new ComponentName(
						info.activityInfo.applicationInfo.packageName,
						info.activityInfo.name), Intent.FLAG_ACTIVITY_NEW_TASK
						| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
				application.icon = info.activityInfo.loadIcon(manager);
				application.packageName = info.activityInfo.applicationInfo.packageName;
				appInfos.add(application);
			}
		}
	}
	
	
	/**
	 * 列表監聽類
	 * @author bill
	 *
	 */
	public final class AppDetailLinster implements OnItemClickListener {

		AlertDialog dialog;

		public void onItemClick(AdapterView view, View arg1,
				final int position, long arg3) {
			AlertDialog.Builder builder = new AlertDialog.Builder(
					MainActivity.this);
			builder.setTitle(選項);
			builder.setItems(R.array.choice, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					final AppInfo appInfo = appInfos.get(position);
					switch (which) {
					case 0: // 啟動程序
						try {
							startApp(appInfo);
						} catch (Exception e) {
							
						}
						break;
					case 1: // 詳細信息
						try {
							showAppDetail(appInfo);
						} catch (Exception e) {
							
						}
						break;
				
					}
					dialog.dismiss();
				}

				private void showAppDetail(AppInfo appInfo)
						throws Exception {
					final String packName = appInfo.packageName.toString();
					final PackageInfo packInfo = getAppPackinfo(packName);
					final String versionName = packInfo.versionName;
					final String[] apppremissions = packInfo.requestedPermissions;
					final String appName = appInfo.title.toString();
					Intent showDetailIntent = new Intent(MainActivity.this,
							ShowAppDetailActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString(packagename, packName);
					bundle.putString(appversion, versionName);
					bundle.putStringArray(apppremissions, apppremissions);
					bundle.putString(appname, appName);
					showDetailIntent.putExtras(bundle);
					startActivity(showDetailIntent);

				}

				private void startApp(AppInfo appInfo)
						throws Exception {
					final String packName = appInfo.packageName.toString();
					final String activityName = getActivityName(packName);
					if (null == activityName) {
						Toast.makeText(MainActivity.this, 程序無法啟動,
								Toast.LENGTH_SHORT);
						return;
					}
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(packName,
							activityName));
					startActivity(intent);
				}

			});
			dialog = builder.create();
			dialog.show();

		}

	}
	/**
	 * 獲取程序信息
	 * @param packName
	 * @return
	 * @throws Exception
	 */
	public PackageInfo getAppPackinfo(String packName) throws Exception {
		return pm.getPackageInfo(packName, PackageManager.GET_ACTIVITIES
				| PackageManager.GET_PERMISSIONS);
	}
	
	/**
	 * 獲取啟動相關程序的Activity
	 * @param packName
	 * @return
	 * @throws Exception
	 */
	public String getActivityName(String packName) throws Exception {
		final PackageInfo packInfo = pm.getPackageInfo(packName,
				PackageManager.GET_ACTIVITIES);
		final ActivityInfo[] activitys = packInfo.activities;
		if (null == activitys || activitys.length <= 0) {
			return null;
		}
		return activitys[0].name;
	}
}


 

app_list.xml:

 






    
    



 

 

app_list_item.xml:

 



	
	
	
		
		
		
		
		
	


 

 

 

/**
 * 查看應用信息
 * @author bill
 *
 */
public class ShowAppDetailActivity extends Activity {

	private TextView tv_appname;
	private TextView tv_appversion;
	private TextView tv_packagename;
	private TextView tv_permission;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_detial);
		tv_appname = (TextView) findViewById(R.id.detail_app_name);
		tv_appversion = (TextView) findViewById(R.id.detail_app_version);
		tv_packagename = (TextView) findViewById(R.id.detail_app_packname);
		tv_permission = (TextView) findViewById(R.id.detail_app_permissions);
		Bundle bundle = this.getIntent().getExtras();
		String packagename=  bundle.getString(packagename);
		String appversion = bundle.getString(appversion);
		String appname = bundle.getString(appname);
		String[] appPremissions = bundle.getStringArray(apppremissions);
		StringBuilder sb = new StringBuilder();
		for(String s : appPremissions){
			sb.append(s);
			sb.append(
);
		}
		tv_appname.setText(appname);
		tv_appversion.setText(appversion);
		tv_packagename.setText(packagename);
		tv_permission.setText(sb.toString());
	}
}
 

 

 

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