单击特定应用程序图标时的Android启动应用程序

时间:2014-10-17 13:30:05

标签: android gridview package-managers

我开发了这样的应用程序,

活动使用gridview显示所有已安装的应用程序及其名称,如下所示:

 private ArrayList<ImageItem> getData() {

    final PackageManager pm = getPackageManager();

    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);


    final ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();

    for (ApplicationInfo packageInfo : packages)

    {
        if (pm.getLaunchIntentForPackage(packageInfo.packageName) != null &&

        !pm.getLaunchIntentForPackage(packageInfo.packageName).equals(""))

        {

            try {
                System.out.println("Application Label :"
                        + pm.getApplicationIcon(packageInfo.packageName)
                                .toString());

                Drawable bitmap = pm
                        .getApplicationIcon(packageInfo.packageName);
                imageItems.add(new ImageItem(bitmap, (String) pm
                        .getApplicationLabel(packageInfo)));
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
    return imageItems;

}

它在所有已安装的应用程序中自定义gridview,但我希望当我点击特定的gridview应用程序图标,如点击Facebook, - &gt;启动那个特定的app,

如何在gridview的onItemClick中获取特定的包启动器以启动特定的应用程序?

如何获取特定应用程序包名称以启动应用程序或如何在gridview中执行操作以获取特定应用程序laucher程序包名称以启动应用程序:

  gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(GridCameraPreviewTestActivity.this,
                    position + "Selected", Toast.LENGTH_SHORT).show();

        }
    });

3 个答案:

答案 0 :(得分:1)

在Gridview上列出所有已安装应用程序的整个代码,并在onItemClick上启动:

public class MainActivity extends Activity implements OnItemClickListener {

private GridView gridview;
private List<ApplicationInfo> applist = null;
private PackageManager packageManager = null;
private ApplicationAdapter listadaptor = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    gridview = (GridView) findViewById(R.id.grid_view);
    packageManager = getPackageManager();
    new LoadApplications().execute();
}

private class LoadApplications extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        Log.d("list size", String.valueOf(applist.size()));
        listadaptor = new ApplicationAdapter(MainActivity.this, R.layout.list_item, applist);
        System.out.println("adapter="+listadaptor);
        return null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(Void result) {

        gridview.setAdapter(listadaptor);
        gridview.setOnItemClickListener(MainActivity.this);

        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    ApplicationInfo app = applist.get(position);
    try {

        Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);

        if (null != intent) {
            startActivity(intent);
        }
    } catch (ActivityNotFoundException e) {
        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
    ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
    for (ApplicationInfo info : list) {
        try {
            if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                applist.add(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
     return applist;
    }
 }

ApplicationAdapter:

public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;

public ApplicationAdapter(Context context, int textViewResourceId,
        List<ApplicationInfo> appsList) {
    super(context, textViewResourceId, appsList);
    this.context = context;
    this.appsList = appsList;
    packageManager = context.getPackageManager();
}

@Override
public int getCount() {
    return ((null != appsList) ? appsList.size() : 0);
}

@Override
public ApplicationInfo getItem(int position) {
    return ((null != appsList) ? appsList.get(position) : null);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (null == view) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_item, null);
    }

    ApplicationInfo data = appsList.get(position);
    if (null != data) {
        TextView appName = (TextView) view.findViewById(R.id.title);
        //TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
        ImageView iconview = (ImageView) view.findViewById(R.id.icon);

        appName.setText(data.loadLabel(packageManager));
        //packageName.setText(data.packageName);
        iconview.setImageDrawable(data.loadIcon(packageManager));
    }
    return view;
   }
 }

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     >

     <GridView
         android:id="@+id/grid_view"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:columnWidth="90dp"
         android:gravity="center"
         android:horizontalSpacing="10dp"
         android:numColumns="auto_fit"
         android:stretchMode="columnWidth"
         android:verticalSpacing="10dp" />

</RelativeLayout>

膨胀xml文件:list_item.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:layout_marginTop="5dp"
       android:orientation="vertical"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="30dp"
            android:layout_height="30dp" >
       </ImageView>

       <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:gravity="left"
            android:textColor="@android:color/white"
            android:textSize="12sp" >
      </TextView>

 </LinearLayout>

我成功运行的整个代码。希望这些对别人有帮助。

答案 1 :(得分:0)

简单:

Intent intent= getPackageManager().getLaunchIntentForPackage("com.example.package");
startActivity(intent);

答案 2 :(得分:0)

首先获取应用程序包名称:

PackageManager pm = getPackageManager(); //get a list of installed apps. List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {

Log.d("TAG", "Installed package :" + packageInfo.packageName);

Log.d("TAG", "Source dir : " + packageInfo.sourceDir);

Log.d("TAG", "Launch Activity:"+pm.getLaunchIntentForPackage(packageInfo.packageName));     }

现在召唤意图:

Intent intent= getPackageManager().getLaunchIntentForPackage("com.example.package"); startActivity(intent);

您可以在Gridview项目点击上管理获取包裹位置和重定向包裹。