这几天项目要添加新功能,这篇博客也就一直推后了,今天趁着接口还没出,抽空补上这篇后续。前一篇写了通过Intent在当前应用分享内容到其他应用,传送门–> 这篇是学习接收其他应用所分享出来的内容。比如可以在自己项目里创建一个Activity,用来接收其他应用分享的内容。
首先创建一个项目,在Manifest.xml中定义Activity可以接收什么样的Intent.我们需要添加Intent过滤器。使用 元素来过滤我们能接收的Intent。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
intent-filter>
<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_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
intent-filter>
activity>
application>
在添加了Intent过滤器后,当其他应用发送如上图所述的Intent时,我们的程序也会列在Intent选择器中,当用户选择了我们的程序后,会调到相对应的Activity中接收数据,我们只需要在Activity中匹配数据再做展示即可。
package com.cxx.rxjavademo;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String action = intent.getAction();//获取Intent的Action
String type = intent.getType();//获取Intent的Type
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith("text/")) {
//我们这里处理所有的文本类型
textReceive(intent);
} else if (type.startsWith("image/")) {
//图片的MIME type有 image/png ,image/jepg, image/gif 等,
imageReceive(intent);
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
MultipleImagesReceive(intent);
}
}
}
private void imageReceive(Intent intent) {
//------如图2
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
ImageView imageView = new ImageView(this);
imageView.setImageURI(imageUri);
setContentView(imageView);
}
}
private void textReceive(Intent intent) {
TextView textView = new TextView(this);
//一般的文本处理,我们直接显示字符串 ------如图1
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
textView.setText(sharedText);
}
//文本文件处理,从Uri中获取输入流,然后将输入流转换成字符串
Uri textUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (textUri != null) {
try {
InputStream inputStream = this.getContentResolver().openInputStream(textUri);
textView.setText(inputStream2Byte(inputStream));
} catch (Exception e) {
e.printStackTrace();
}
}
//设置给Activity
setContentView(textView);
}
/**
* 将输入流转换成字符串
*
* @param inputStream
* @return
* @throws IOException
*/
private String inputStream2Byte(InputStream inputStream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
//指定编码格式为UIT-8
return new String(bos.toByteArray(), "UTF-8");
}
private void MultipleImagesReceive(Intent intent) {
//------如图3
final ArrayList imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
GridView gridView = new GridView(this);
//设置列为自动适应
gridView.setNumColumns(GridView.AUTO_FIT);
gridView.setAdapter(new GridAdapter(this, imageUris));
setContentView(gridView);
}
}
/**
* 重写BaseAdapter
*/
public class GridAdapter extends BaseAdapter {
private Context mContext;
private ArrayList list;
public GridAdapter(Context mContext, ArrayList list) {
this.list = list;
this.mContext = mContext;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(500, 500));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageURI(list.get(position));
return imageView;
}
}
}