【问题标题】:Android - Show an app chooser of all of the installed appsAndroid - 显示所有已安装应用程序的应用程序选择器
【发布时间】:2014-07-12 13:10:06
【问题描述】:

我想打开一个应用程序选择器,它会列出手机上已安装的应用程序。在选择器中单击应用程序时,我不需要打开它,而是获取它的包名称。

我知道我可以创建一个特定的意图,例如使用 Intent.ACTION_SEND 和

Intent.createChooser()

我也知道我可以使用包管理器列出已安装的包。

有没有一种方法可以结合这两种方法并为所有已安装的应用创建一个应用选择器?

【问题讨论】:

    标签: java android android-intent android-listview


    【解决方案1】:

    Chooser.java

    public class Chooser extends ListActivity {
          AppAdapter adapter=null;
    
          @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.chooser_layout);
    
            PackageManager pm=getPackageManager();
            Intent main=new Intent(Intent.ACTION_MAIN, null);
    
            main.addCategory(Intent.CATEGORY_LAUNCHER);
            List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);
            Collections.sort(launchables,
                             new ResolveInfo.DisplayNameComparator(pm));
    
            adapter=new AppAdapter(pm, launchables);
            setListAdapter(adapter);
          }
    
          @Override
          protected void onListItemClick(ListView l, View v,
                                         int position, long id) {
    
            ResolveInfo launchable=adapter.getItem(position);
            ActivityInfo activity=launchable.activityInfo;
            ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                                 activity.name);
    
            /* SetResult 
    
            String pack_name = name.getPackageName();
    
            Intent intentMessage=new Intent();
            intentMessage.putExtra("MESSAGE_package_name", pack_name);
            setResult(1,intentMessage);
            finish();
    
            */
    
    
    
    
          }
    
          class AppAdapter extends ArrayAdapter<ResolveInfo> {
            private PackageManager pm=null;
    
            AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
              super(Chooser.this, R.layout.row, apps);
              this.pm=pm;
            }
    
            @Override
            public View getView(int position, View convertView,
                                  ViewGroup parent) {
              if (convertView==null) {
                convertView=newView(parent);
              }
    
              bindView(position, convertView);
    
              return(convertView);
            }
    
            private View newView(ViewGroup parent) {
              return(getLayoutInflater().inflate(R.layout.row, parent, false));
            }
    
            private void bindView(int position, View row) {
              TextView label=(TextView)row.findViewById(R.id.label);
    
              label.setText(getItem(position).loadLabel(pm));
    
              ImageView icon=(ImageView)row.findViewById(R.id.icon);
    
              icon.setImageDrawable(getItem(position).loadIcon(pm));
            }
          }
    
    
        }
    

    chooser_layout.xml

        <?xml version="1.0" encoding="utf-8"?>
        <ListView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
        />
    

    row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >
      <ImageView android:id="@+id/icon"
          android:drawable="@drawable/ic_launcher"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:paddingLeft="2px"
        android:paddingTop="3px"
        android:paddingBottom="3px"
        android:paddingRight="3px"
      />
      <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:textSize="18sp"
        android:text="Test App will here bla bla"
        android:paddingTop="2px"
        android:paddingBottom="2px"
      />
    
    </RelativeLayout>
    

    您可以选择这样的应用:

    Intent appIntent;
    ...
    
    appIntent=new Intent(this,Chooser.class);
                startActivityForResult(appIntent, 1);
    
    
    
    ...
    
    
     @Override
         protected void onActivityResult(int requestCode, int resultCode, Intent data)
         {
    
           super.onActivityResult(requestCode, resultCode, data);
    
           if(null!=data){
    
             if(requestCode==1){
                //Do something
               String message=data.getStringExtra("MESSAGE_package_name");
               package_Name.SetText(message);
             }
         }
    

    这对你有用。

    【讨论】:

      【解决方案2】:

      使用 PackageManager 和 queryIntentActivities() 的结果来填充您自己的 UI 并列出它们。 This sample application 将它们加载到 ListView 中。

      当示例应用程序转身并在onListItemClick() 中启动选定的活动时,您可以执行其他操作。

      【讨论】:

      • 这显然可以工作,但它没有利用操作系统内置的漂亮样式的应用程序选择器。尝试用图标、2 列等设计一个漂亮的 ListView UI 似乎是在浪费时间......
      • @Yuval:欢迎您在您的 SDK 平台中找到其中的一些资源,将它们复制到您的项目中,然后重复使用它们(遵守 Apache 许可证)。据我所知,没有内置的选择器可以简单地告诉您选择的活动。您看到的 UI 是一个带有 ACTION_CHOOSER 的活动,而不是在您自己的进程中运行的某个实用程序对话框。
      • 如何找到 Intent.createChooser() 中使用的 xml 和适配器?
      猜你喜欢
      • 2011-11-04
      • 2016-05-11
      • 1970-01-01
      • 2015-09-17
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多