分享现在有许多集成的,如友盟,ShareSDK等,或者直接去下载哪个App的分享SDK来集成,这些分享都是会提示来自xxx(appName)。如果不需要,可以简单通过Intent.ACTION_SEND来实现分享功能
下面通过一段代码来看看分享的实现
public static void share(Context context, String extraText) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, extraText);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(
Intent.createChooser(shareIntent, context.getString(R.string.action_share)));
}
可以看到,仅只需要传送上下文和内容,一次分享动作就实现
分享纯文本
如上面,调用ShareUtil.share(this, "test share");
就实现了分享文本
分享图片
public static void share(Context context, Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
context.startActivity(
Intent.createChooser(shareIntent, context.getString(R.string.action_share)));
}
调用方式:
File file = BitmapUtil.Drawable2File(this, R.mipmap.ic_launcher, Environment.getExternalStorageDirectory() + "/test.png");
uri = BitmapUtil.File2Uri(file);
ShareUtil.share(this, uri);
注意:这里有写SD卡动作,需要添加读写权限,6.0添加运行时判断
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
public static void share(Context context, ArrayList uris) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
shareIntent.setType("image/*");
context.startActivity(
Intent.createChooser(shareIntent, context.getString(R.string.action_share)));
}
调用方式:ShareUtil.share(this, uris);
从分享动作可以看出,只是发了一个Intent出来,所以其它App也是没有硬性要求接收你的分享数据 – 不写接口咯,但我们可以自己写个接收分享数据接口出来,也是几行代码。
AndroidManifest.xml
添加intent-filter
<activity android:name=".ShareReceiveActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
intent-filter>
activity>
public class ShareReceiveActivity extends AppCompatActivity {
@Bind(R.id.type)
TextView type;
@Bind(R.id.text)
TextView text;
@Bind(R.id.image)
ImageView image;
@Bind(R.id.image_multiple)
ImageView imageMultiple;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_receive);
ButterKnife.bind(this);
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
type.setText("type:text");
text.setText(sharedText);
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Update UI to reflect image being shared
type.setText("type:image");
image.setImageURI(imageUri);
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
type.setText("type:multiple");
if (imageUris.size() == 2) {
image.setImageURI(imageUris.get(0));
imageMultiple.setImageURI(imageUris.get(1));
}
}
}
}
下载