美文网首页Android知识
Android实现分享和接收分享内容

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

作者: 学海摆渡人 | 来源:发表于2016-09-17 23:22 被阅读4239次

    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);
        }
    }
    

    相关文章

      网友评论

        本文标题:Android实现分享和接收分享内容

        本文链接:https://www.haomeiwen.com/subject/sfuwettx.html