用Intent.ACTION_SEND进行分享

分享现在有许多集成的,如友盟,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<Uri> 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<Uri> 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));
            }
        }
    }
}

下载

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值