简体   繁体   中英

Android - how to implement a share button with a message?

I am trying to implement a simple share action that would show the user a menu of Facebook, twitter, and email where they can share a set message I pre-set like "I am using a great app on http://www.xyz.com

What I have so far is the styling:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_share"
          android:title="@string/share"
          android:showAsAction="ifRoom"
          android:actionProviderClass="android.widget.ShareActionProvider" />
</menu> 

and I see many examples like this online:

Intent intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

// Add data to the intent, the receiving app will decide what to do with it.
intent.putExtra(Intent.EXTRA_SUBJECT, “Some Subject Line”);
intent.putExtra(Intent.EXTRA_TEXT, “Body of the message, woot!”);   

But that just opens the email. But how would I open that menu with all the possible places the text and link can be shared?

Thanks!

String message = "I am using a great app on http://www.xyz.com"
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, message);

startActivity(Intent.createChooser(i, "Share with friends"));

But that just opens the email

Then either your timing of using this Intent is incorrect, or the only app on the device advertising support for ACTION_SEND of text/plain is "email".

But how would I open that menu with all the possible places the text and link can be shared?

The following activity, from this sample application , run on my Galaxy Nexus, shows 8 possible sharing destinations, some built in (Bluetooth, Messaging) and some third-party (Remember the Milk, Evernote):

public class MainActivity extends SherlockFragmentActivity implements
    ShareActionProvider.OnShareTargetSelectedListener, TextWatcher {
  private ShareActionProvider share=null;
  private Intent shareIntent=new Intent(Intent.ACTION_SEND);
  private EditText editor=null;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    shareIntent.setType("text/plain");
    editor=(EditText)findViewById(R.id.editor);
    editor.addTextChangedListener(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.actions, menu);

    share=
        (ShareActionProvider)menu.findItem(R.id.share)
                                 .getActionProvider();
    share.setOnShareTargetSelectedListener(this);

    return(super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onShareTargetSelected(ShareActionProvider source,
                                       Intent intent) {
    Toast.makeText(this, intent.getComponent().toString(),
                   Toast.LENGTH_LONG).show();

    return(false);
  }

  @Override
  public void afterTextChanged(Editable s) {
    shareIntent.putExtra(Intent.EXTRA_TEXT, editor.getText());
    share.setShareIntent(shareIntent);
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count,
                                int after) {
    // ignored
  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before,
                            int count) {
    // ignored
  }
}

Note that you need to call setShareIntent() on the ShareActionProvider when the text to be shared is ready. In my case, that comes from an EditText widget, and so needs to be updated whenever that text changes, which is why I have the setShareIntent() call in onTextChanged() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM