1

Is it possible to listing Intent.ACTION_SEND ? I mean that I need to know if someone share on Facebook or tweet on Twitter by action_send.

goodm
  • 7,275
  • 6
  • 31
  • 55
  • what exactly you want to implement? – Paresh Mayani Mar 14 '12 at 12:32
  • I have "share" button what was using to post on user's facebook wall, and I have DialogListener to know if user really share or just press the button and exit. I want to have possibility to share also to Twitter or just sms, but I need to know if user really share to increase his points. – goodm Mar 14 '12 at 12:36

2 Answers2

2

Maybe you wanted a more complete answer because the accepted one was rather short, I'm a year too late but hopefully it's still useful :)

So here's a possible solution in handling multiple intents...

1) You want to know the result (eg succes or fail) of the intent?

Just start the intent using following line:

startActivityForResult(intent, 1); //instead of startActivity(intent)

And retrieve the requestCode and resultCode by overriding onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if(resultCode == Activity.RESULT_OK){
            //intent 0 = succesful (Facebook)
        } else{
            //intent 0 = failed or canceled
        }
    } else if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            //intent 1 = succesful (Twitter)
        } else{
            //intent 1 = failed or canceled
        }
    }
}

2) You want to know which app the intent opened?

Don' trust the built-in intent chooser, make your own dialog and give each intent another requestCode (a unique integer-value, to identify the intent)

An example:

new AlertDialog.Builder(this)
.setTitle("Share with friends!")
.setSingleChoiceItems(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,
    new String[]{"Facebook", "Twitter"}), -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    StartFacebookShare();
                } else if (which == 1) {
                    StartTwitterShare();
                }
                dialog.dismiss();
            }
}).show();

private void StartFacebookShare() {
    String messageUrl = "http://www.stackoverflow.com"; //the url you want to share
    try {
        Intent intent = new Intent("android.intent.category.SEND");
        intent.putExtra(Intent.EXTRA_TEXT, messageUrl);
        intent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity");
        startActivityForResult(intent, 0);
    } catch (Exception e) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://m.facebook.com/sharer.php?u=" + messageUrl));
        startActivityForResult(intent, 1);
    }
}
private void StartTwitterShare() {
    String messageUrl= "http://www.stackoverflow.com"; //the string you want to tweet
    try {
        //see edit below for more info on API
        Twitter twitter = TwitterFactory.getSingleton();
        Status status = twitter.updateStatus(messageUrl);
    } catch (Exception e) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + messageUrl));
        startActivityForResult(intent, 1);
    }
}

Some useful info can be found here and here, maybe search here or comment if you have suggestions for my code (I always like feedback ^^) or if you're stuck on something :)

Edit: Some minor changes such as the intents and always added a catch with a web intent (can't fail now, right?) And for the Twitter part I used a jar (place it in your 'libs' folder) and the Twitter API which needs following registration and configuration, good luck!

Community
  • 1
  • 1
T_D
  • 3,241
  • 3
  • 17
  • 24
  • 1
    Nice piece of code. Don't need it right now, but I'm sure gonna need it in future! Thanks – goodm Apr 27 '13 at 19:32
  • Never hardcode class and package names from third-party apps that are not part of some documented and supported API. – CommonsWare Apr 27 '13 at 19:41
  • @CommonsWare Agreed. See edit, is this somewhat the solution you had in mind? – T_D Apr 29 '13 at 09:01
2

No, you have no way of determining whether a person actually does anything in the application that you link to via ACTION_SEND. This is very similar to the Web, where you have no idea whether a person does anything in the Web site that you link to via a URL.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491