How to filter specific apps for ACTION_SEND intent (and set a different text for each app)

后端 未结 12 1913
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 01:08

How can you filter out specific apps when using the ACTION_SEND intent? This question has been asked in various ways, but I haven\'t been able to gather a

12条回答
  •  故里飘歌
    2020-11-22 01:27

    This solution shows a list of applications in a ListView dialog that resembles the chooser:

    screenshot

    It is up to you to:

    1. obtain the list of relevant application packages
    2. given a package name, invoke the relevant intent

    The adapter class:

    import java.util.List;
    
    import android.content.Context;
    import android.content.pm.ApplicationInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.graphics.drawable.Drawable;
    import android.util.TypedValue;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    
    public class ChooserArrayAdapter extends ArrayAdapter {
        PackageManager mPm;
        int mTextViewResourceId;
        List mPackages;
    
        public ChooserArrayAdapter(Context context, int resource, int textViewResourceId, List packages) {
            super(context, resource, textViewResourceId, packages);
            mPm = context.getPackageManager();
            mTextViewResourceId = textViewResourceId;
            mPackages = packages;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String pkg = mPackages.get(position);
            View view = super.getView(position, convertView, parent);
    
            try {
                ApplicationInfo ai = mPm.getApplicationInfo(pkg, 0);
    
                CharSequence appName = mPm.getApplicationLabel(ai);
                Drawable appIcon = mPm.getApplicationIcon(pkg);
    
                TextView textView = (TextView) view.findViewById(mTextViewResourceId);
                textView.setText(appName);
                textView.setCompoundDrawablesWithIntrinsicBounds(appIcon, null, null, null);
                textView.setCompoundDrawablePadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
    
            return view;
        }
    
    }
    

    and its usage:

        void doXxxButton() {
            final List packages = ...;
            if (packages.size() > 1) {
                ArrayAdapter adapter = new ChooserArrayAdapter(MyActivity.this, android.R.layout.select_dialog_item, android.R.id.text1, packages);
    
                new AlertDialog.Builder(MyActivity.this)
                .setTitle(R.string.app_list_title)
                .setAdapter(adapter, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item ) {
                        invokeApplication(packages.get(item));
                    }
                })
                .show();
            } else if (packages.size() == 1) {
                invokeApplication(packages.get(0));
            }
        }
    
        void invokeApplication(String packageName) {
            // given a package name, create an intent and fill it with data
            ...
            startActivityForResult(intent, rq);
        }
    

提交回复
热议问题