下面列出了怎么用android.widget.ShareActionProvider的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_menu_browserwebfragment, menu);
MenuItem item = menu.findItem(R.id.menu_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
refreshItem = menu.findItem(R.id.menu_refresh);
super.onCreateOptionsMenu(menu, inflater);
if (Utility.isWeiboAccountDomainLink(mUrl)) {
String result = Utility.getDomainFromWeiboAccountLink(mUrl);
Intent intent = new Intent(getActivity(), UserInfoActivity.class);
intent.putExtra("domain", result);
getActivity().startActivity(intent);
getActivity().finish();
} else if (Utility.isWeiboMid(mUrl)) {
String mid = Utility.getMidFromUrl(mUrl);
RedirectLinkToWeiboIdTask task = new RedirectLinkToWeiboIdTask(BrowserWebFragment.this, mUrl, mid);
task.executeOnExecutor(MyAsyncTask.THREAD_POOL_EXECUTOR);
} else {
mWebView.loadUrl(mUrl);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
ShareActionProvider shareActionProvider = (ShareActionProvider) item
.getActionProvider();
// set the log content as share content
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
getResources().getString(R.string.action_share_subject));
shareIntent.putExtra(Intent.EXTRA_TEXT, AppController.getInstance()
.getLog());
shareIntent.setType("text/plain");
shareActionProvider.setShareIntent(shareIntent);
return true;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (BuildConfig.DEBUG) Log.d(getLogTag(), "onCreateOptionsMenu");
inflater.inflate(R.menu.photoviewer_menu, menu);
mFavoriteButton = menu.findItem(R.id.menu_favorite);
mWallpaperButton = menu.findItem(R.id.menu_set_wallpaper);
/* The task could return before this has inflated, so make sure it's up
* to date */
if (mPhotoExtendedInfo != null) {
updateFavoriteButtonIcon(mPhotoExtendedInfo.isFavorite());
}
/* Set file with share history to the provider and set the share
* intent. */
MenuItem shareActionItem = menu.findItem(R.id.menu_share);
ShareActionProvider shareActionProvider =
(ShareActionProvider) shareActionItem.getActionProvider();
shareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
shareActionProvider.setShareIntent(createShareIntent());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_menu_browserwebfragment, menu);
MenuItem item = menu.findItem(R.id.menu_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
refreshItem = menu.findItem(R.id.menu_refresh);
super.onCreateOptionsMenu(menu, inflater);
if (Utility.isWeiboAccountDomainLink(mUrl)) {
String result = Utility.getDomainFromWeiboAccountLink(mUrl);
Intent intent = new Intent(getActivity(), UserInfoActivity.class);
intent.putExtra("domain", result);
getActivity().startActivity(intent);
getActivity().finish();
} else if (Utility.isWeiboMid(mUrl)) {
String mid = Utility.getMidFromUrl(mUrl);
RedirectLinkToWeiboIdTask task = new RedirectLinkToWeiboIdTask(BrowserWebFragment.this, mUrl, mid);
task.executeOnExecutor(MyAsyncTask.THREAD_POOL_EXECUTOR);
} else {
mWebView.loadUrl(mUrl);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate your menu.
getMenuInflater().inflate(R.menu.action_bar_share_action_provider, menu);
// Set file with share history to the provider and set the share intent.
MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
// Note that you can set/change the intent any time,
// say when the user has selected an image.
actionProvider.setShareIntent(createShareIntent());
// Set file with share history to the provider and set the share intent.
MenuItem overflowItem = menu.findItem(R.id.menu_item_share_action_provider_overflow);
ShareActionProvider overflowProvider =
(ShareActionProvider) overflowItem.getActionProvider();
overflowProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
// Note that you can set/change the intent any time,
// say when the user has selected an image.
overflowProvider.setShareIntent(createShareIntent());
return true;
}
@Override
public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent)
{
int currentIndex = mFilmstripController.getCurrentAdapterIndex();
if (currentIndex < 0)
{
return false;
}
UsageStatistics.instance()
.mediaInteraction(fileNameFromAdapterAtIndex(currentIndex), MediaInteraction.InteractionType.SHARE,
NavigationChange.InteractionCause.BUTTON, fileAgeFromAdapterAtIndex(currentIndex));
// TODO add intent.getComponent().getPackageName()
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
shareAction = menu.findItem(R.id.menu_action_share);
shareAction.setVisible(mPrefs.getBoolean(SHOW_SHARE_BUTTON, false));
mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
updateShareIntent();
mMenu = menu;
return super.onCreateOptionsMenu(menu);
}
public static void setShareIntent(Activity activity, ShareActionProvider mShareActionProvider, String content) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
if (Utility.isIntentSafe(activity, shareIntent) && mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_menu_browserweibocommentactivity, menu);
MenuItem item = menu.findItem(R.id.menu_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
buildShareActionMenu();
super.onCreateOptionsMenu(menu, inflater);
}
public static void setShareIntent(Activity activity, ShareActionProvider mShareActionProvider, MessageBean msg) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
if (msg != null && msg.getUser() != null) {
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "@" + msg.getUser().getScreen_name() + ":" + msg.getText());
if (!TextUtils.isEmpty(msg.getThumbnail_pic())) {
Uri picUrl = null;
String smallPath = FileManager.getFilePathFromUrl(msg.getThumbnail_pic(),
FileLocationMethod.picture_thumbnail);
String middlePath = FileManager.getFilePathFromUrl(msg.getBmiddle_pic(), FileLocationMethod.picture_bmiddle);
String largePath = FileManager.getFilePathFromUrl(msg.getOriginal_pic(), FileLocationMethod.picture_large);
if (ImageUtility.isThisBitmapCanRead(largePath)) {
picUrl = Uri.fromFile(new File(largePath));
} else if (ImageUtility.isThisBitmapCanRead(middlePath)) {
picUrl = Uri.fromFile(new File(middlePath));
} else if (ImageUtility.isThisBitmapCanRead(smallPath)) {
picUrl = Uri.fromFile(new File(smallPath));
}
if (picUrl != null) {
shareIntent.putExtra(Intent.EXTRA_STREAM, picUrl);
shareIntent.setType("image/*");
}
}
if (Utility.isIntentSafe(activity, shareIntent) && mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
}
public static void setShareIntent(Activity activity, ShareActionProvider mShareActionProvider, String content) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
if (Utility.isIntentSafe(activity, shareIntent) && mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
setActionbarVisibility(true);
getActivity().getMenuInflater().inflate(R.menu.action_file, menu);
getActivity().getMenuInflater().inflate(R.menu.action_file_single, menu);
MenuItem shareMenuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) shareMenuItem.getActionProvider();
this.preserveSelection = false;
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
setIntent("This is example text");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
//Share the name of the pizza
TextView textView = (TextView)findViewById(R.id.pizza_text);
CharSequence pizzaName = textView.getText();
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, pizzaName);
shareActionProvider.setShareIntent(intent);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
setIntent("This is example text");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
setIntent("This is example text");
return super.onCreateOptionsMenu(menu);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.tweet, menu);
MenuItem share = menu.findItem(R.id.share);
mShareActionProvider = (ShareActionProvider) share.getActionProvider();
mShareActionProvider.setShareIntent(mShareIntent);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
mShare = menu.add(Menu.NONE, R.id.action_share, Menu.NONE, R.string.share);
mShare.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
mShareActionProvider = new ShareActionProvider(getActivity());
mShareActionProvider.setShareIntent(mIntent);
mShare.setActionProvider(mShareActionProvider);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fantasy, menu);
mShare = menu.findItem(R.id.action_share);
if (!Intent.ACTION_MAIN.equals( getActivity().getIntent().getAction())) {
menu.findItem(R.id.home).setVisible(false);
}
mShareActionProvider = (ShareActionProvider) mShare.getActionProvider();
mShare.setActionProvider(mShareActionProvider);
mShareActionProvider.setShareIntent(mIntent);
}
public static void setShareIntent(Activity activity, ShareActionProvider mShareActionProvider, String content) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
if (Utility.isIntentSafe(activity, shareIntent) && mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_menu_browserweibocommentactivity, menu);
MenuItem item = menu.findItem(R.id.menu_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
buildShareActionMenu();
super.onCreateOptionsMenu(menu, inflater);
}
public static void setShareIntent(Activity activity, ShareActionProvider mShareActionProvider, MessageBean msg) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
if (msg != null && msg.getUser() != null) {
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "@" + msg.getUser().getScreen_name() + ":" + msg.getText());
if (!TextUtils.isEmpty(msg.getThumbnail_pic())) {
Uri picUrl = null;
String smallPath = FileManager.getFilePathFromUrl(msg.getThumbnail_pic(),
FileLocationMethod.picture_thumbnail);
String middlePath = FileManager.getFilePathFromUrl(msg.getBmiddle_pic(), FileLocationMethod.picture_bmiddle);
String largePath = FileManager.getFilePathFromUrl(msg.getOriginal_pic(), FileLocationMethod.picture_large);
if (ImageUtility.isThisBitmapCanRead(largePath)) {
picUrl = Uri.fromFile(new File(largePath));
} else if (ImageUtility.isThisBitmapCanRead(middlePath)) {
picUrl = Uri.fromFile(new File(middlePath));
} else if (ImageUtility.isThisBitmapCanRead(smallPath)) {
picUrl = Uri.fromFile(new File(smallPath));
}
if (picUrl != null) {
shareIntent.putExtra(Intent.EXTRA_STREAM, picUrl);
shareIntent.setType("image/*");
}
}
if (Utility.isIntentSafe(activity, shareIntent) && mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
}
public static void setShareIntent(Activity activity, ShareActionProvider mShareActionProvider, String content) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
if (Utility.isIntentSafe(activity, shareIntent) && mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actions, menu);
getMenuInflater().inflate(R.menu.common, menu);
share=
(ShareActionProvider)menu.findItem(R.id.share)
.getActionProvider();
shareIntent.putExtra(Intent.EXTRA_TEXT,
getString(R.string.msg_test));
share.setShareIntent(shareIntent);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
provider = (ShareActionProvider) menu.findItem(R.id.menu_share)
.getActionProvider();
doShare();
return true;
}
private void prepareShareActionItem(MenuItem shareItem) {
shareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
shareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
shareActionProvider.setOnShareTargetSelectedListener(shareTargetSelectedListener);
shareActionProvider.setShareIntent(prepareShareIntent(video));
}
@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
Log.d(LOG_TAG, "onShareTargetSelected: " + intent.getStringExtra(Intent.EXTRA_TEXT));
String lowerFqn = intent.getComponent().getClassName().toLowerCase(Locale.US);
// Twitter
if (lowerFqn.contains("twitter") || lowerFqn.contains("tweet")) {
// Twitter clients use the EXTRA_TEXT only, with 140c limit.
int tweetLength = 140;
String tweetFormat = "just learned about \"%s\" (%s) via @khanacademy";
// As an example, the following tweet is 127 characters.
// just learned about 'Lebron Asks: What are the chances of making 10 free throws in a row?' (20 char link is here) via @khanacademy
// Links in tweets take exactly 20 characters (https://support.twitter.com/articles/78124-how-to-post-links-urls-in-tweets).
int linkLength = 20;
int usedLength = tweetFormat.length() - "%s".length() + linkLength; // We know the length the link will take.
usedLength -= "%s".length(); // The title will replace an instance of this string.
// Truncate the title to fit the tweet.
int remainingSpace = tweetLength - usedLength;
String title = intent.getStringExtra("title");
if (title.length() > remainingSpace) {
title = title.substring(0, remainingSpace - 3) + "...";
}
String tweet = String.format(tweetFormat, title, intent.getStringExtra("url"));
intent.putExtra(Intent.EXTRA_TEXT, tweet);
}
else {
String subject = "just learned about " + intent.getStringExtra("title");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
String bodyFormat = "\"%s\" (%s) is one of nearly 4,000 great educational videos at http://www.khanacademy.org/.";
String body = String.format(bodyFormat, intent.getStringExtra("title"), intent.getStringExtra("url"));
String desc = intent.getStringExtra("desc");
if (desc != null && desc.length() > 0) {
body += "\n\nVideo description: " + desc;
}
intent.putExtra(Intent.EXTRA_TEXT, body);
}
// In docs for this method: "NOTE: Modifying the intent is not permitted and any changes to the latter will be ignored."
// Since we need to modify the intent (that's the whole point of this callback, isn't it?!), we launch the activity ourselves.
// Launch in new task, hopefully avoiding the case where the user already has facebook open to another activity and we don't see the share activity.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}