7

I wonder, is there any way to choose behavior using Intent.createChooser method? For example, I have an image, which I would like send by e-mail, if it's chosen (first option). And on second option I'd like to send sms with the link on this image (For which I'll need complex actions - upload image to the sever, retrieve download link, which I'd like to be in the sms and paste it to the sms)

Could you possibly come up with any suggestion, what should I do to accomplish the second task?

I believe I can send an e-mail with image with something like this:

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: I realized, that what I truly needed is to intercept the user click, if the sms was chosen in intent chooser. So, the question is how it might be accomplished?

sabadow
  • 5,095
  • 3
  • 34
  • 51
xvar
  • 103
  • 1
  • 1
  • 7
  • http://stackoverflow.com/a/15022153/909497 Hope this helps. – ACengiz Oct 10 '13 at 14:13
  • @ACengiz, thank you, but I have no difficulty with sending an e-mail. The question is how to get Chooser for both - sms and e-mail. Difference between them is that I want to send e-mail with the image itself and sms with the link to the image. – xvar Oct 10 '13 at 14:26

3 Answers3

10

1)Create Intent to perform share or send operation,

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)Create AlertDialog to set the Application in the 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)Get the list of application related to the particular intent using the ResolveInfo

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

4))Set the list of application to the custom listview.

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

5)Finally,lanch the particular application when choose the application from the list of application in 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);

For more refer this link:http://velmuruganandroidcoding.blogspot.in/2014/01/custom-chooser-android-in-example.html

user3921740
  • 520
  • 6
  • 7
  • 1
    This answer references variables that aren't declared. What's `pm`, and how does the reader create it? Why does the `AppAdapter` need `pm` in its constructor? – wsgeorge Apr 04 '17 at 14:39
  • @wsgeorge Appears that the code was adapted from https://github.com/commonsguy/cw-advandroid/blob/master/Introspection/Launchalot/src/com/commonsware/android/launchalot/Launchalot.java. – Edric May 20 '19 at 05:29
1

It seems to me, that it can't be accomplished exactly as I wanted.

Possible way is to build custom app chooser using queryIntentActivities() in the PackageManager class. Helpful post: Custom filtering of intent chooser based on installed Android package name

Another possible way is to create custom pop-up - http://developer.android.com/guide/topics/ui/menus.html#PopupMenu
or floating context menu -
http://developer.android.com/guide/topics/ui/menus.html#FloatingContextMenu

It appeared to be that what customer actually wanted was only some custom Dialog. Something like this:

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();
}
Community
  • 1
  • 1
xvar
  • 103
  • 1
  • 1
  • 7
0

In addition to the selected answer.
You could also add a control for Facebook, as Facebook does not work well with the 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);
                }
Community
  • 1
  • 1