loadApp 点击应用的图标启动该应用实现的原理
package com.example.loadapp;?import java.util.ArrayList;import java.util.List;?import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.pm.ActivityInfo;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.graphics.drawable.Drawable;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListAdapter;import android.widget.TextView;?public class MainActivity extends Activity{?? ? private List<ResolveInfo> list;? ? private ListAdapter adapter=new BaseAdapter()? ? {? ? ? ??? ? ? ? @Override? ? ? ? public View getView(int position, View convertView, ViewGroup parent)? ? ? ? {? ? ? ? ? ? LayoutInflater inflater = getLayoutInflater();? ? ? ? ? ? LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.item, null);? ? ? ? ? ? ImageView imageView = (ImageView)linearLayout.findViewById(R.id.imageView1);? ? ? ? ? ? TextView textView = (TextView)linearLayout.findViewById(R.id.textView1);? ? ? ? ? ?// imageView.setLayoutParams(new AbsListView.LayoutParams(96,96));? ? ? ? ? ? APP app = APPlist.get(position);? ? ? ? ? ? imageView.setImageDrawable(app.getIcon());? ? ? ? ? ? ?textView.setText(app.getLabel());? ? ? ? ? ? return linearLayout;? ? ? ? }? ? ? ??? ? ? ? @Override? ? ? ? public long getItemId(int position)? ? ? ? {? ? ? ? ? ? // TODO Auto-generated method stub? ? ? ? ? ? return 0;? ? ? ? }? ? ? ??? ? ? ? @Override? ? ? ? public Object getItem(int position)? ? ? ? {? ? ? ? ? ? // TODO Auto-generated method stub? ? ? ? ? ? return null;? ? ? ? }? ? ? ??? ? ? ? @Override? ? ? ? public int getCount()? ? ? ? {? ? ? ? ? ? return APPlist.size();? ? ? ? }? ? };? ? private ArrayList<APP> APPlist;? ??? ? @Override? ? protected void onCreate(Bundle savedInstanceState)? ? {? ? ? ? super.onCreate(savedInstanceState);? ? ? ? setContentView(R.layout.activity_main);? ? ? ? GridView gridView =(GridView) findViewById(R.id.gridView1);? ? ? ? ?APPlist = new ArrayList<APP>();? ? ? ? PackageManager packageManager = getPackageManager();? ? ? ? Intent intent=new Intent(Intent.ACTION_MAIN);? ? ? ? intent.addCategory(Intent.CATEGORY_LAUNCHER);? ? ? ? list = packageManager.queryIntentActivities(intent, 0);? ? ? ? for (int i = 0; i <list.size(); i++)? ? ? ? {? ? ? ? ? ?ActivityInfo activityInfo = list.get(i).activityInfo;? ? ? ? Drawable icon = activityInfo.loadIcon(getPackageManager());? ? ? ? ? ?CharSequence label = activityInfo.loadLabel(getPackageManager());? ? ? ? ? ?String packagename=activityInfo.packageName;? ? ? ? ? ?//android:name ?就是該activity的類名? ? ? ? ? ?//android:name ?就是該activity的類名? ? ? ? ? ?//android:name ?就是該activity的類名? ? ? ? ? ?String itemname=activityInfo.name;? ? ? ? ? ?? ? ? ? ? APP app = new APP(icon,label,packagename,itemname);?? ? ? ? ? APPlist.add(app);? ? ? ? }? ? ? ? gridView.setAdapter(adapter);? ? ? ? gridView.setOnItemClickListener(new OnItemClickListener()? ? ? ? {?? ? ? ? ? ? @Override? ? ? ? ? ? public void onItemClick(AdapterView<?> parent, View view,? ? ? ? ? ? ? ? ? ? int position, long id)? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? Intent intent002 = new Intent(Intent.ACTION_MAIN);? ? ? ? ? ? ? ? intent002.addCategory(Intent.CATEGORY_LAUNCHER);? ? ? ? ? ? ? ? APP app = APPlist.get(position);? ? ? ? ? ? ? ? intent002.setPackage(app.getPackagename());? ? ? ? ? ? ? ? //return ? intent ?該方法是非常的重要和有用? ? ? ? ? ? ? ? //return ? intent ?該方法是非常的重要和有用? ? ? ? ? ? ? ? //return ? intent ?該方法是非常的重要和有用? ? ? ? ? ? ? ? //return ? intent ?該方法是非常的重要和有用? ? ? ? ? ? ? ? intent002.setComponent(new ComponentName(app.getPackagename(), app.getItemname()));? ? ? ? ? ? ? ? startActivity(intent002); ? ? ? ? ? ?? ? ? ? ? ? }? ? ? ? });? ? ? ??? ? }?? ? @Override? ? public boolean onCreateOptionsMenu(Menu menu)? ? {? ? ? ? // Inflate the menu; this adds items to the action bar if it is present.? ? ? ? getMenuInflater().inflate(R.menu.activity_main, menu);? ? ? ? return true;? ? }?}///////////////////////////////////////////////app的详细信息package com.example.loadapp;?import android.graphics.drawable.Drawable;?public class APP{?? ? private Drawable icon;? ? private CharSequence label;? ? private String packagename;? ? private String itemname;?? ? public APP(Drawable icon, CharSequence label, String packagename, String itemname)? ? {? ? ? ? this.icon=icon;? ? ? ? this.label=label;? ? ? ? this.packagename=packagename;? ? ? ? this.itemname=itemname;? ? }?? ? /**? ? ?* @return ?就是包中要啟動的類 的 ?類名? ? ?*/? ? public String getItemname()? ? {? ? ? ? return itemname;? ? }?? ? /**? ? ?* @param itemname ?就 是包中要啟動的類的 類名? ? ?*/? ? public void setItemname(String itemname)? ? {? ? ? ? this.itemname = itemname;? ? }?? ? public String getPackagename()? ? {? ? ? ? return packagename;? ? }?? ? public void setPackagename(String classname)? ? {? ? ? ? this.packagename = classname;? ? }?? ? public Drawable getIcon()? ? {? ? ? ? return icon;? ? }?? ? public void setIcon(Drawable icon)? ? {? ? ? ? this.icon = icon;? ? }?? ? public CharSequence getLabel()? ? {? ? ? ? return label;? ? }?? ? public void setLabel(CharSequence label)? ? {? ? ? ? this.label = label;? ? }? ???}