【问题标题】:Android launch application when click on perticular application-icon单击特定应用程序图标时 Android 启动应用程序
【发布时间】:2014-10-17 13:30:05
【问题描述】:

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

Activity 使用 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,->启动那个特定的应用程序,

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

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

  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();

        }
    });

【问题讨论】:

    标签: android gridview package-managers


    【解决方案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;
        }
     }
    

    应用适配器:

    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>
    

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

    【讨论】:

      【解决方案2】:

      就这么简单:

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

      【讨论】:

      • 但我点击任何应用程序,所有应用程序都有不同的包名吗?
      • 是的,您必须拥有所有应用程序的列表,并且您拥有选定的职位。这样你就可以得到选择的包名并使用它。
      【解决方案3】:

      首先获取Application的包名:

      PackageManager pm = getPackageManager(); //get a list of installed apps. List&lt;ApplicationInfo&gt; 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 项目单击时管理获取包位置和重定向包。

      【讨论】:

      • 感谢您的回复,但我已经完成了这项工作,但我想要 gridview onclick 中的内容
      • 请使用 BaseAdapter 或 ArrayAdapter 来显示您的 Gridview 数据,这样更容易获得特定的应用程序位置及其重定向。请参考:theopentutorials.com/tutorials/android/listview/…
      猜你喜欢
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多