【问题标题】:Android. Custom Intent chooser安卓。自定义意图选择器
【发布时间】:2013-10-10 14:06:53
【问题描述】:

我想知道,有没有办法使用Intent.createChooser 方法来选择行为? 例如,我有一张图像,如果选择了它,我想通过电子邮件发送它(第一个选项)。在第二个选项中,我想发送sms 与此图片上的链接 (为此我需要复杂的操作 - 将图像上传到服务器,检索下载链接,我想在 sms 中并将其粘贴到 sms

您能否提出任何建议,我应该怎么做才能完成第二个任务?

我相信我可以发送一封带有类似图片的电子邮件:

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..."));

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

【问题讨论】:

  • stackoverflow.com/a/15022153/909497 希望这会有所帮助。
  • @ACengiz,谢谢,但我发送电子邮件没有困难。问题是如何为短信和电子邮件获取选择器。它们之间的区别在于我想发送带有图像本身的电子邮件和带有图像链接的短信。

标签: android android-intent


【解决方案1】:

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,在alertdialog中设置Application,

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)最后,从listview的应用程序列表中选择应用程序时,启动特定应用程序,

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

【讨论】:

【解决方案2】:

在我看来,它无法完全按照我的意愿完成。

可能的方法是使用 PackageManager 类中的 queryIntentActivities() 构建自定义应用选择器。 有用的帖子:Custom filtering of intent chooser based on installed Android package name

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

似乎客户真正想要的只是一些自定义Dialog。像这样的:

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

【讨论】:

    【解决方案3】:

    除了the selected answer.
    您还可以为 Facebook 添加一个控件,因为 Facebook 不能很好地与共享者一起使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 2011-08-09
      • 2012-12-11
      • 2014-12-01
      • 1970-01-01
      相关资源
      最近更新 更多