下面列出了怎么用android.support.v4.app.ShareCompat的API类实例代码及写法,或者点击链接到github查看源代码。
private void shareApp() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(String.format(getString(R.string.app_share), getPackageName()))
.getIntent();
if (shareIntent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.action_share)));
}
/*Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getString(R.string.app_share), getPackageName()));
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.action_share)));
*/
/*Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getString(R.string.app_share), getPackageName()));
sendIntent.setType("text/plain");
startActivity(sendIntent);*/
}
public void sharePhoto() {
Uri uri = albumItem.getUri(this);
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.addStream(uri)
.setType(MediaType.getMimeType(this, uri))
.getIntent();
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String title = getString(R.string.share_item, albumItem.getType(this));
if (shareIntent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(shareIntent, title));
} else {
String error = getString(R.string.share_error, albumItem.getType(this));
Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
viewModel = ViewModelProviders.of(this).get(CartViewModel.class);
fab = (FloatingActionButton) findViewById(R.id.fab);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setupToolbar();
handleDeepLink();
fab.setImageDrawable(VectorDrawableCompat.create(getResources(), R.drawable.ic_share_white_24dp, null));
fab.setOnClickListener(view -> {
String cartId = viewModel.getCartId().getValue();
ShareCompat.IntentBuilder.from(this)
.setText(String.format(Locale.US, "Check out my shopping cart now using Android Instant Apps! \n%s/cart/%s", ROOT_ENDPOINT, cartId))
.setType("text/plain")
.setChooserTitle(share_cart)
.startChooser();
});
}
@Override
protected void onPostExecute(File result) {
if (result == null) {
return;
}
// glide cache uses an unfriendly & extension-less name,
// massage it based on the original
// result.renameTo(renamed);
Uri uri = FileProvider.getUriForFile(activity,
activity.getString(R.string.share_authority), result);
ShareCompat.IntentBuilder.from(activity)
.setText(getShareText())
.setType(getImageMimeType(result.getName()))
.setSubject(shot.title)
.setStream(uri)
.startChooser();
}
@Override
protected void onPostExecute(File result) {
if (result == null) { return; }
// glide cache uses an unfriendly & extension-less name,
// massage it based on the original
String fileName = shot.images.best();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
File renamed = new File(result.getParent(), fileName);
result.renameTo(renamed);
Uri uri = FileProvider.getUriForFile(activity, BuildConfig.FILES_AUTHORITY, renamed);
ShareCompat.IntentBuilder.from(activity)
.setText(getShareText())
.setType(getImageMimeType(fileName))
.setSubject(shot.title)
.setStream(uri)
.startChooser();
}
@Override public void onShareUserBackup(@NonNull MainView mainView, @NonNull FirebaseUser currentUser) {
String packageName = mainView.getApplicationContext().getPackageName();
Uri deepLinkBuilder = new Uri.Builder()
.scheme("http")
.authority(BuildConfig.FA_HOST)
.appendQueryParameter(BuildConfig.SHARED_URI, currentUser.getUid())
.build();
Uri.Builder builder = new Uri.Builder()
.scheme("https")
.authority(mainView.getResources().getString(R.string.link_ref) + ".app.goo.gl")
.path("/")
.appendQueryParameter("link", Uri.parse(deepLinkBuilder.toString()).toString())
.appendQueryParameter("apn", packageName);
ShareCompat.IntentBuilder.from(mainView)
.setType("message/*")
.setSubject(mainView.getString(R.string.sharing_backup))
.setChooserTitle(mainView.getString(R.string.share_my_backup))
.setHtmlText("<a href='" + Uri.decode(builder.toString()) + "'>" + mainView.getString(R.string.click_here_html) +
"</a><br/><b>~" + mainView.getString(R.string.app_name) + "</b>").startChooser();
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.share:
Video video = ((WatchMeAdapterNew.WatchMeHolder) getRecycleView()
.findViewHolderForPosition(mSelectedVideoPosition)).getObject();
ShareCompat.IntentBuilder.from(getActivity())
.setChooserTitle(R.string.abc_shareactionprovider_share_with)
.setType("text/plain")
.setText(getString(R.string.sharing_video_template, video.getTitle(), video.getLinkForShare()))
.startChooser();
return true;
default:
return false;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("packageName,versionCode,versionName\n");
for (int i = 0; i < adapter.getItemCount(); i++) {
App app = adapter.getItem(i);
if (app != null) {
stringBuilder.append(app.packageName).append(',')
.append(app.installedVersionCode).append(',')
.append(app.installedVersionName).append('\n');
}
}
ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(this)
.setSubject(getString(R.string.send_installed_apps))
.setChooserTitle(R.string.send_installed_apps)
.setText(stringBuilder.toString())
.setType("text/csv");
startActivity(intentBuilder.getIntent());
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_github:
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(Config.GITHUB_URL));
startActivity(browse);
return true;
case R.id.action_social:
ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(MainActivity.this);
intentBuilder.setChooserTitle("Choose Share App")
.setType("text/plain")
.setSubject("Flat button for android")
.setText("A flat button library for android #AndroidFlat goo.gl/C6aLDi")
.startChooser();
return true;
}
return super.onOptionsItemSelected(item);
}
public static void shareFile(Activity context, File f) {
try {
Uri uri = Uri.fromFile(f);
ShareCompat.IntentBuilder.from(context)
.setStream(uri)
.setType(URLConnection.guessContentTypeFromName(f.getName()))
.startChooser();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public void shareText(View view) {
String txt = mShareTextEditText.getText().toString();
String mimeType = "text/plain";
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle("Share this text with: ")
.setText(txt)
.startChooser();
}
public void shareText(View view) {
String txt = mShareTextEditText.getText().toString();
String mimeType = "text/plain";
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle("Share this text with: ")
.setText(txt)
.startChooser();
}
private static void createTempFileAndShare(AppCompatActivity context, String fileShareAuthority, String csvString, String csvFileName, String type) {
try {
File fileDir = new File(context.getFilesDir(), "sqliteManager");
fileDir.mkdir();
File file = new File(fileDir, csvFileName);
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(csvString.getBytes());
fileOutputStream.close();
// generate URI, defined authority as the application ID in the Manifest
Uri uriToCSVFIle = FileProvider.getUriForFile(context, fileShareAuthority, file);
Intent shareIntent =
ShareCompat.IntentBuilder
.from(context)
.setStream(uriToCSVFIle)
.setType(type)
.getIntent();
// Provide read access
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// validate that the device can open your File!
PackageManager pm = context.getPackageManager();
if (shareIntent.resolveActivity(pm) != null) {
context.startActivity(shareIntent);
}
} catch (Exception e) {
// e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refresh) {
swipeRefreshLayout.setRefreshing(true);
StatusSavingService.performFetch(mContext);
return true;
} else if (id == R.id.action_help) {
showHelpPopup(MainActivity.this);
return true;
} else if (id == R.id.action_share) {
String mimeType = "text/plain";
String title = "Share WhatsApp Status Saver App";
ShareCompat.IntentBuilder.from(this)
.setType(mimeType)
.setChooserTitle(title)
.setText(getResources().getString(R.string.share_text))
.startChooser();
return true;
} else if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
private void shareText (String text) {
// COMPLETED (2) Create a String variable called mimeType and set it to "text/plain"
String mimeType = "text/plain";
// COMPLETED (3) Create a title for the chooser window that will pop up
String title = "Share with:";
// COMPLETED (4) Use ShareCompat.IntentBuilder to build the Intent and start the chooser
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle(title)
.setText(text)
.startChooser();
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. We set the
* type of content that we are sharing (just regular text), the text itself, and we return the
* newly created Intent.
*
* @return The Intent to use to start our share.
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecast + FORECAST_SHARE_HASHTAG)
.getIntent();
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. We set the
* type of content that we are sharing (just regular text), the text itself, and we return the
* newly created Intent.
*
* @return The Intent to use to start our share.
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecast + FORECAST_SHARE_HASHTAG)
.getIntent();
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. We set the
* type of content that we are sharing (just regular text), the text itself, and we return the
* newly created Intent.
*
* @return The Intent to use to start our share.
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecast + FORECAST_SHARE_HASHTAG)
.getIntent();
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. We set the
* type of content that we are sharing (just regular text), the text itself, and we return the
* newly created Intent.
*
* @return The Intent to use to start our share.
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecast + FORECAST_SHARE_HASHTAG)
.getIntent();
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. We set the
* type of content that we are sharing (just regular text), the text itself, and we return the
* newly created Intent.
*
* @return The Intent to use to start our share.
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecast + FORECAST_SHARE_HASHTAG)
.getIntent();
return shareIntent;
}
/**
* Uses the ShareCompat Intent builder to create our Forecast intent for sharing. All we need
* to do is set the type, text and the NEW_DOCUMENT flag so it treats our share as a new task.
* See: http://developer.android.com/guide/components/tasks-and-back-stack.html for more info.
*
* @return the Intent to use to share our weather forecast
*/
private Intent createShareForecastIntent() {
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(mForecastSummary + FORECAST_SHARE_HASHTAG)
.getIntent();
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
return shareIntent;
}