Android. 自定义意图选择器

7
我想知道是否有办法使用Intent.createChooser方法来选择行为? 例如,我有一张图片,如果选择第一个选项,我想通过电子邮件发送它。如果选择第二个选项,我想发送带有该图片链接的短信(需要复杂的操作-上传图片到服务器,检索下载链接,并将其粘贴到短信中)。
你能否提出任何建议,如何完成第二个任务?
我相信我可以像这样发送带有图像的电子邮件:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{textMail}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Some Subj"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Some Extra Text"); 
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileUri));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

更新: 我意识到,我真正需要的是拦截用户点击事件,如果在意图选择器中选择了sms。那么问题是如何实现呢?


希望这可以帮到你:https://dev59.com/UGoy5IYBdhLWcg3wN7YX#15022153 - ACengiz
@ACengiz,谢谢你,但是我发送电子邮件没有困难。问题是如何同时获取短信和电子邮件的选择器。它们之间的区别在于我想发送带有图像本身的电子邮件和带有图像链接的短信。 - xvar
3个回答

10

1)创建Intent以执行共享或发送操作。

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"velmurugan@androidtoppers.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "Hi");
email.putExtra(Intent.EXTRA_TEXT, "Hi,This is Test");

email.setType("text/plain");

2)创建AlertDialog以在对话框中设置应用程序,

final Dialog dialog = new Dialog(Custom_chooser.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(WMLP);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.about_dialog);
dialog.show();

3)使用ResolveInfo获取与特定意图相关的应用程序列表。

List<ResolveInfo> launchables=pm.queryIntentActivities(email, 0);
Collections.sort(launchables,newResolveInfo.DisplayNameComparator(pm));

4)将应用程序列表设置为自定义的列表视图。

adapter=new AppAdapter(pm, launchables);
lv.setAdapter(adapter);

5) 最后,在应用程序列表中选择应用程序,然后启动该特定应用程序。

ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
email.addCategory(Intent.CATEGORY_LAUNCHER);
email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
email.setComponent(name);
startActivity(email);

更多信息请参考此链接:http://velmuruganandroidcoding.blogspot.in/2014/01/custom-chooser-android-in-example.html


1
这个答案涉及到未声明的变量。pm是什么,读者如何创建它?为什么AppAdapter在其构造函数中需要pm - wsgeorge
@wsgeorge 看起来这段代码是从 https://github.com/commonsguy/cw-advandroid/blob/master/Introspection/Launchalot/src/com/commonsware/android/launchalot/Launchalot.java 改编而来的。 - Edric

1

我觉得,它不能完全按照我的意愿实现。

可能的方法是使用PackageManager类中的queryIntentActivities()构建自定义应用选择器。 有用的帖子: 基于已安装Android包名称的自定义过滤器intent选择器

另一种可能的方法是创建自定义弹出窗口 - http://developer.android.com/guide/topics/ui/menus.html#PopupMenu
或浮动上下文菜单 -
http://developer.android.com/guide/topics/ui/menus.html#FloatingContextMenu

事实上,客户想要的只是一些自定义对话框。就像这样:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("E-mail / MMS").setItems(R.array.send_array, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // The 'which' argument contains the index position
            // of the selected item
        }
    });
    return builder.create();
}

0
除了所选答案之外,你还可以添加一个Facebook控件,因为Facebook在sharer上无法正常工作。
if (activity.applicationInfo.packageName.toLowerCase().contains("facebook")) {
                    //Share on Facebook
                    ShareLinkContent content = new ShareLinkContent.Builder().
                            setContentUrl(Uri.parse(mLink)).
                            setImageUrl(Uri.parse(mImageURL)).
                                    setContentTitle(mTitle).
                                    setContentDescription(mDescription)
                            .build();
                    com.facebook.share.widget.ShareDialog.show(mActivity, content);
                } else {
                    //Share on selected application
                    ComponentName name = new ComponentName(activity.applicationInfo.packageName,
                            activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                            Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    mActivity.startActivity(shareIntent);
                }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接