我想知道,有没有办法使用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
,我真正需要的是拦截用户点击。那么,问题是如何实现它?
答案 0 :(得分:10)
1)创建意图以执行共享或发送操作,
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中设置应用程序
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 :(得分:1)
在我看来,它无法完全按照我的意愿完成。
可能的方法是使用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();
}
答案 2 :(得分:0)
除了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);
}