Gabriel Esteban
Gabriel Esteban

Reputation: 762

How to create an android share intent that can work with all clients?

Hi I'm working on app and I want to publish a tweet with a photo using a Share intent. First I search and I found an intent that works with the official client, and what I want is that my app work with all clients.

Here it's my code:

    String message = "Here goes my message";
    Uri screenshotUri = Uri.parse("file:///sdcard/screenshot.jpeg");
    try {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setClassName(getApplicationContext(),findTwitterClient().getPackage());
        sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        startActivity(sharingIntent);
    } catch (Exception e) {
        Log.e("In Exception", "Comes here");

        //This also opens a dialog that

    }

Also I have the following function to know what client is installed on the device.

public Intent findTwitterClient() {
    final String[] twitterApps = {
            // package // name - nb installs (thousands)
            "com.twitter.android", // official - 10 000
            "com.twidroid", // twidroyd - 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<ResolveInfo> 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;
}

Upvotes: 1

Views: 318

Answers (2)

kabuko
kabuko

Reputation: 36302

You can't guarantee that you can share with all Twitter clients. Intents are a sort of interface between applications. Each client decides for itself which intents it supports handling. You could easily imagine a custom Twitter client that doesn't handle any intents other than the main launch one.

Upvotes: 1

dannyroa
dannyroa

Reputation: 5571

If your URL starts with "http://twitter.com", Android will ask the user what app should the tweet be opened with. Pretty much any app that registers for that URL scheme can be used.

Upvotes: 1

Related Questions