开发者

How do I let a User choose from two different Intents?

开发者 https://www.devze.com 2023-02-22 13:38 出处:网络
I have two intents that I would like Users to choose from.One intent is for driving directions via Google Maps and the other is through Google Navigation (Beta).This is how I set 开发者_如何学Pythonup

I have two intents that I would like Users to choose from. One intent is for driving directions via Google Maps and the other is through Google Navigation (Beta). This is how I set 开发者_如何学Pythonup each Intent:

 //regular maps
 Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
                   Uri.parse("geo:0,0?q=40.7143528,-74.0059731"));
//navigation
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
           Uri.parse("google.navigation:40.7143528,-74.0059731"));

I think I have to use Intent.createChooser() but I'm not sure how to implement it with specific intents. I've used it before with email like so:

startActivity(Intent.createChooser(emailIntent, "Send your email in:")); 

where emailIntent is set up here:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

Thank you in advance!!!!


You can custom your Intent.createChooser to show only the Itents you want.

The trick is add EXTRA_INITIAL_INTENTS to the default list, generated from the createChoose, and remove the others Intents from the list.

Look at this sample where I create a chooser that shows only my e-mails apps. In my case appears three mails: Gmail, YahooMail and the default Mail.

private void share(String nameApp, String imagePath) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
            targetedShare.setType("image/jpeg"); // put here your mime type

            if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || 
                    info.activityInfo.name.toLowerCase().contains(nameApp)) {
                targetedShare.putExtra(Intent.EXTRA_SUBJECT,  "My e-mail my-subject");
                targetedShare.putExtra(Intent.EXTRA_TEXT,     "My body of post/email");
                targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
                targetedShare.setPackage(info.activityInfo.packageName);
                targetedShareIntents.add(targetedShare);
            }
        }

        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
    }
}

You can run like that: share("mail", "/sdcard/dcim/Camera/photo.jpg");

In your case, just add to targetedShareIntents yours 2 Intents.

This was based on post: Custom filtering of intent chooser based on installed Android package name


You have to do it manually. createChooser opens a dialog with a list of apps that can handle one specific intent. On the other hand, what you have is two different intents... so just create a dialog with the two options. Take a look at this example.

0

精彩评论

暂无评论...
验证码 换一张
取 消