How to filter specific apps for ACTION_SEND intent (and set a different text for each app)

后端 未结 12 1912
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 01:08

How can you filter out specific apps when using the ACTION_SEND intent? This question has been asked in various ways, but I haven\'t been able to gather a

12条回答
  •  無奈伤痛
    2020-11-22 01:45

    Found a solution that works for me looking here (see the third comment on the first answer). This code looks for a valid twitter client and uses it to post the tweet. Note: It does not give you an Intent with the various Twitter clients and allow you to choose.

    Share using twitter:

    Intent shareIntent = findTwitterClient(); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, "test");
    startActivity(Intent.createChooser(shareIntent, "Share"));
    

    Calling this method:

    public Intent findTwitterClient() {
        final String[] twitterApps = {
                // package // name - nb installs (thousands)
                "com.twitter.android", // official - 10 000
                "com.twidroid", // twidroid - 5 000
                "com.handmark.tweetcaster", // Tweecaster - 5 000
                "com.thedeck.android" }; // TweetDeck - 5 000 };
        Intent tweetIntent = new Intent();
        tweetIntent.setType("text/plain");
        final PackageManager packageManager = getPackageManager();
        List list = packageManager.queryIntentActivities(
                tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
    
        for (int i = 0; i < twitterApps.length; i++) {
            for (ResolveInfo resolveInfo : list) {
                String p = resolveInfo.activityInfo.packageName;
                if (p != null && p.startsWith(twitterApps[i])) {
                    tweetIntent.setPackage(p);
                    return tweetIntent;
                }
            }
        }
    
        return null;
    }
    

    Facebook will be similar using "com.facebook.katana", although you still can't set the message text (deprecated July 2011).

    Code source: Intent to open twitter client on Android

提交回复
热议问题