Android實(shí)現(xiàn)分享和接收分享內(nèi)容

Android實(shí)現(xiàn)分享文本:(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實(shí)現(xiàn)分享圖片:(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實(shí)現(xiàn)分享多個(gè)圖片:(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.."));

現(xiàn)在就讓我們的應(yīng)用可以接收其他應(yīng)用分享過來的內(nèi)容:
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、現(xiàn)在我們的Activity可以接收到分享的內(nèi)容了,現(xiàn)在可以去分別處理獲取到的數(shù)據(jù):

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);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,549評論 0 17
  • 什么是json? JSON是一種基于文本的數(shù)據(jù)交換方式,或者叫做數(shù)據(jù)描述格式,你是否該選用他首先肯定要關(guān)注它所擁有...
    LiLi原上草閱讀 361評論 0 4
  • 趁著周五沒啥事,一下午畫了一張小丑,臨摹的是《蝙蝠俠:致命玩笑》的漫畫。 工具是鉛筆、彩鉛、中性筆、橡皮擦(是的我...
    魔鬼的贊歌閱讀 1,908評論 28 23
  • 2017年3月20日 今天的咨詢體會最深的是我的完美主義,處處都有我的完美,對人對己高標(biāo)嚴(yán)苛。出了門之后就...
    Indigolove閱讀 178評論 0 0