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