暂存测试几个用intent打开分享的平台
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.yunbao.common.server;
|
||||
|
||||
import static android.content.Intent.EXTRA_CHOSEN_COMPONENT;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
public class ShareBroadcastReceiver extends BroadcastReceiver {
|
||||
public static final int REQUEST_CODE = 888;
|
||||
private static final String TAG = "分享log";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.i(TAG, "onReceive");
|
||||
Log.i(TAG, intent.getAction() + " | " + intent.getParcelableExtra(EXTRA_CHOSEN_COMPONENT));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,53 @@
|
||||
package com.yunbao.common.utils;
|
||||
|
||||
import static android.app.PendingIntent.FLAG_IMMUTABLE;
|
||||
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
|
||||
import static android.content.Intent.EXTRA_CHOSEN_COMPONENT;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.yunbao.common.server.ShareBroadcastReceiver;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 分享工具
|
||||
*/
|
||||
public class ShareUtil {
|
||||
public static void share(String content) {
|
||||
share(content, null);
|
||||
|
||||
public static void share(Context context, String content) {
|
||||
share(context, content, null);
|
||||
}
|
||||
|
||||
public static void share(Bitmap bitmap) {
|
||||
share(null,bitmap);
|
||||
public static void share(Context context, File image) {
|
||||
share(context, null, image);
|
||||
}
|
||||
|
||||
public static void share(String content, Bitmap bitmap) {
|
||||
public static void share(Context context, String content, File image) {
|
||||
Intent shareIntent = new Intent(Intent.ACTION_SEND);
|
||||
|
||||
if (image != null) {
|
||||
Uri uri = Uri.fromFile(image);
|
||||
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
|
||||
shareIntent.setType("image/*");
|
||||
//当用户选择短信时使用sms_body取得文字
|
||||
shareIntent.putExtra("sms_body", content);
|
||||
} else {
|
||||
shareIntent.setType("text/plain");
|
||||
}
|
||||
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
|
||||
//自定义选择框的标题
|
||||
PendingIntent pi = PendingIntent.getBroadcast(context, ShareBroadcastReceiver.REQUEST_CODE,
|
||||
new Intent(context, ShareBroadcastReceiver.class), PendingIntent.FLAG_MUTABLE);
|
||||
context.registerReceiver(new ShareBroadcastReceiver(),new IntentFilter(EXTRA_CHOSEN_COMPONENT));
|
||||
shareIntent = Intent.createChooser(shareIntent, null, pi.getIntentSender());
|
||||
context.startActivity(Intent.createChooser(shareIntent, "Share"));
|
||||
//系统默认标题
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user