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