Android实现分享和接收分享内容

Android实现分享文本:(Intent):

Intent sendIntent =newIntent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

Android实现分享图片:(Intent):

Intent shareIntent =newIntent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Android实现分享多个图片:(Intent):

ArrayList imageUris =newArrayList();
imageUris.add(imageUri1);// Add your image URIs here
imageUris.add(imageUri2);
Intent shareIntent =newIntent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"Share images to.."));

现在就让我们的应用可以接收其他应用分享过来的内容:
1、首先需要在接收分享信息的界面的清单文件中注册接收的ACTION:

<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>

2、现在我们的Activity可以接收到分享的内容了,现在可以去分别处理获取到的数据:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import java.util.ArrayList;

class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action)&&type!=null){
            if ("text/plain".equals(type)){
                dealTextMessage(intent);
            }else if(type.startsWith("image/")){
                dealPicStream(intent);
            }
        }else if (Intent.ACTION_SEND_MULTIPLE.equals(action)&&type!=null){
            if (type.startsWith("image/")){
                dealMultiplePicStream(intent);
            }
        }
    }

    void dealTextMessage(Intent intent){
        String share = intent.getStringExtra(Intent.EXTRA_TEXT);
        String title = intent.getStringExtra(Intent.EXTRA_TITLE);
    }

    void dealPicStream(Intent intent){
        Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    }

    void dealMultiplePicStream(Intent intent){
        ArrayList<Uri> arrayList = intent.getParcelableArrayListExtra(intent.EXTRA_STREAM);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,050评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,859评论 0 17
  • 什么是json? JSON是一种基于文本的数据交换方式,或者叫做数据描述格式,你是否该选用他首先肯定要关注它所拥有...
    LiLi原上草阅读 2,833评论 0 4
  • 趁着周五没啥事,一下午画了一张小丑,临摹的是《蝙蝠侠:致命玩笑》的漫画。 工具是铅笔、彩铅、中性笔、橡皮擦(是的我...
    魔鬼的赞歌阅读 5,872评论 28 23
  • 2017年3月20日 今天的咨询体会最深的是我的完美主义,处处都有我的完美,对人对己高标严苛。出了门之后就...
    Indigolove阅读 1,387评论 0 0

友情链接更多精彩内容