简体   繁体   English

Android-显示所有已安装应用的应用选择器

[英]Android - Show an app chooser of all of the installed apps

I want to open an app chooser that will list of the installed apps on the phone. 我想打开一个应用选择器,该选择器将列出手机上已安装的应用。 When clicking on an app in the chooser I don't need to open it but get it's package name. 在选择器中单击某个应用程序时,我不需要打开它,但会得到它的包名。

I know that I can create a specific intent for example with Intent.ACTION_SEND and the 我知道我可以使用例如Intent.ACTION_SEND和

Intent.createChooser()

I also know that I can list the installed packages using the package manager. 我也知道我可以使用软件包管理器列出已安装的软件包。

Is there a way to combine the 2 methods and create an app chooser with all of the installed apps? 有没有一种方法可以将这两种方法结合起来,并与所有已安装的应用程序一起创建应用程序选择器?

Chooser.java 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 choicer_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 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>

You can choose app like this: 您可以选择这样的应用程序:

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

This will work for you. 这将为您工作。

Use the results of PackageManager and queryIntentActivities() to populate your own UI that lists them. 使用PackageManagerqueryIntentActivities()的结果来填充列出它们的UI。 This sample application loads them into a ListView . 此示例应用程序将它们加载到ListView

While that sample application turns around and launches the selected activity in onListItemClick() , you could do something else instead. 当该示例应用程序转身并在onListItemClick()启动选定的活动时,您可以执行其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM