应用分享内容到其他应用是Android最常见的形式。如下图,将文本分享到其他应用。
我为此分装了一个类,使用也格外简单。
一. 分享到其他应用包装类
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import java.util.List;
/**
* Created by: qinliyang
* Date: 2020/8/24 - 2:42 PM
*/
public class ShareHelper {
private static final String TAG = "ShareHelper";
/**
* Current activity
*/
private Activity activity;
/**
* Share content type
*/
private @ShareContentType
String contentType;
/**
* Share title
*/
private String title;
/**
* Share file Uri
*/
private Uri shareFileUri;
/**
* Share content text
*/
private String contentText;
/**
* Share to special component PackageName
*/
private String componentPackageName;
/**
* Share to special component ClassName
*/
private String componentClassName;
/**
* Share complete onActivityResult requestCode
*/
private int requestCode;
/**
* Forced Use System Chooser
*/
private boolean forcedUseSystemChooser;
public ShareHelper(@NonNull Builder builder) {
this.activity = builder.activity;
this.contentType = builder.contentType;
this.title = builder.title;
this.shareFileUri = builder.shareFileUri;
this.contentText = builder.textContent;
this.componentPackageName = builder.componentPackageName;
this.componentClassName = builder.componentClassName;
this.requestCode = builder.requestCode;
this.forcedUseSystemChooser = builder.forcedUseSystemChooser;
}
/**
* shareBySystem
*/
public void shareBySystem() {
if (checkShareParam()) {
Intent shareIntent = createShareIntent();
if (shareIntent == null) {
Log.e(TAG, "shareBySystem cancel.");
return;
}
if (title == null) {
title = "";
}
if (forcedUseSystemChooser) {
shareIntent = Intent.createChooser(shareIntent, title);
}
if (shareIntent.resolveActivity(activity.getPackageManager()) != null) {
try {
if (requestCode != -1) {
activity.startActivityForResult(shareIntent, requestCode);
} else {
activity.startActivity(shareIntent);
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
}
}
private Intent createShareIntent() {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.addCategory("android.intent.category.DEFAULT");
if (!TextUtils.isEmpty(this.componentPackageName) && !TextUtils.isEmpty(componentClassName)) {
ComponentName comp = new ComponentName(componentPackageName, componentClassName);
shareIntent.setComponent(comp);
}
switch (contentType) {
case ShareContentType.TEXT:
shareIntent.putExtra(Intent.EXTRA_TEXT, contentText);
shareIntent.setType("text/plain");
break;
case ShareContentType.IMAGE:
case ShareContentType.AUDIO:
case ShareContentType.VIDEO:
case ShareContentType.FILE:
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addCategory("android.intent.category.DEFAULT");
shareIntent.setType(contentType);
shareIntent.putExtra(Intent.EXTRA_STREAM, shareFileUri);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.d(TAG, "Share uri: " + shareFileUri.toString());
break;
default:
Log.e(TAG, contentType + " is not support share type.");
shareIntent = null;
break;
}
return shareIntent;
}
private boolean checkShareParam() {
if (this.activity == null) {
Log.e(TAG, "activity is null.");
return false;
}
if (TextUtils.isEmpty(this.contentType)) {
Log.e(TAG, "Share content type is empty.");
return false;
}
if (ShareContentType.TEXT.equals(contentType)) {
if (TextUtils.isEmpty(contentText)) {
Log.e(TAG, "Share text context is empty.");
return false;
}
} else {
if (this.shareFileUri == null) {
Log.e(TAG, "Share file path is null.");
return false;
}
}
return true;
}
public static class Builder {
private Activity activity;
private @ShareContentType
String contentType = ShareContentType.FILE;
private String title;
private String componentPackageName;
private String componentClassName;
private Uri shareFileUri;
private String textContent;
private int requestCode = -1;
private boolean forcedUseSystemChooser = true;
public Builder(Activity activity) {
this.activity = activity;
}
/**
* Set Content Type
*
* @param contentType {@link ShareContentType}
* @return Builder
*/
public Builder setContentType(@ShareContentType String contentType) {
this.contentType = contentType;
return this;
}
/**
* Set Title
*
* @param title title
* @return Builder
*/
public Builder setTitle(@NonNull String title) {
this.title = title;
return this;
}
/**
* Set share file path
*
* @param shareFileUri shareFileUri
* @return Builder
*/
public Builder setShareFileUri(Uri shareFileUri) {
this.shareFileUri = shareFileUri;
return this;
}
/**
* Set text content
*
* @param textContent textContent
* @return Builder
*/
public Builder setTextContent(String textContent) {
this.textContent = textContent;
return this;
}
/**
* Set Share To Component
*
* @param componentPackageName componentPackageName
* @param componentClassName componentPackageName
* @return Builder
*/
public Builder setShareToComponent(String componentPackageName, String componentClassName) {
this.componentPackageName = componentPackageName;
this.componentClassName = componentClassName;
return this;
}
/**
* Set onActivityResult requestCode, default value is -1
*
* @param requestCode requestCode
* @return Builder
*/
public Builder setOnActivityResult(int requestCode) {
this.requestCode = requestCode;
return this;
}
/**
* Forced Use System Chooser To Share
*
* @param enable default is true
* @return Builder
*/
public Builder forcedUseSystemChooser(boolean enable) {
this.forcedUseSystemChooser = enable;
return this;
}
/**
* build
*
* @return Share2
*/
public ShareHelper build() {
return new ShareHelper(this);
}
}
}
二. 使用如下:
new ShareHelper.Builder(activity)
.setOnActivityResult(SHARE_REQUEST_CODE)
// 指定分享的文件类型
.setContentType(ShareContentType.TEXT)
.setTextContent(shareText)
.build()
// 发起分享
.shareBySystem();
可以根据自己想要分享的内容。 文字,图片,视频等。【可能有些应用不支持,因此需要测试】