一。清单文件相关配置,配置入口Activity的exported属性为true,使第三方应用可以唤醒我们的app,添加两个intent-filter用来接收浏览器发过来的数据
,acticion为android.intent.action.SEND,mimeType携带的是分享类型。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="com.feinno.cake.pushmessage" />
<category android:name="android.intent.category.DEFAULT" />
</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" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
二.在MainActivity的onCreate方法里面做相应处理。首先取出对应的数据,在这里我只适配了部分浏览器。
if ("android.intent.action.SEND".equals(getIntent()action)){
//说明是浏览器分享过来的
showWhereShare();
}
private void showWhereShare() {
String url = getIntent().getExtras().getString("url");
String text = getIntent().getExtras().getString(Intent.EXTRA_TEXT);
String type = getIntent().getType();
String title = getIntent().getExtras().getString(Intent.EXTRA_TITLE);
title = title == null ? text : title;
final Uri imageUri = getIntent().getExtras().getParcelable(Intent.EXTRA_STREAM);
String filePath = getIntent().getExtras().getString("file");
if (url == null && text == null && title == null && imageUri == null){
ToastUtils.showShortToast(this,R.string.cant_share);
}
Gson gson = new Gson();
String brand = android.os.Build.BRAND;
if ("Xiaomi".equals(brand) && url == null){
//适配小米浏览器
if (url == null && !TextUtils.isEmpty(text) && text.contains("http")){
String [] datas = text.split("http");
String newsUrl = "http" + datas[1];
if (newsUrl!=null && Patterns.WEB_URL.matcher(newsUrl.toString()).matches()){
title = datas[0];
text = datas[0];
if (title.contains("【")){
title = title.split("【")[1].split("】")[0];
}
url = newsUrl;
}
}
}
if ("image/png".equals(type) && !TextUtils.isEmpty(text) && text.contains("http")){
//百度浏览器
String [] datas = text.split("http");
String newsUrl = "http" + datas[1];
if (newsUrl!=null && Patterns.WEB_URL.matcher(newsUrl.toString()).matches()){
title = datas[0];
url = newsUrl;
}
}
final String shareInfo = gson.toJson(new SharePictureBean(url,text,type,title,imageUri == null ? "":imageUri.toString(),filePath));
//链接分享
if("text/plain".equals(type) && !TextUtils.isEmpty(url) && !TextUtils.isEmpty(title)){
//do something
return;
}
if ("image/*".equals(type)&& !TextUtils.isEmpty(url)&&!TextUtils.isEmpty(text)){
//适配QQ浏览器
//do something
return;
}
if ("image/png".equals(type) && !TextUtils.isEmpty(text) && text.contains("http")){
//百度浏览器
//do something
return;
}
//文本分享
if ("text/plain".equals(type)&&!TextUtils.isEmpty(text) && url == null){
//do something
return;
}
//图片分享
if(type.startsWith("image/") && TextUtils.isEmpty(text) && imageUri != null){
//do something
return;
}
}
三。用到的bean类
package com.feinnoui.library.ui.entity;
public class SharePictureBean {
public String url;
public String text;
public String type;
public String title;
public String imageUri;
public String filePath;
public SharePictureBean(String url, String text, String type, String title, String imageUri, String filePath) {
this.url = url;
this.text = text;
this.type = type;
this.title = title;
this.imageUri = imageUri;
this.filePath = filePath;
}
}
四,适配的浏览器总结。
浏览器 返回数据
小米浏览器 只有一个text 里面包含url,标题,来源 正常
QQ浏览器 返回 url uri text title 正常
UC浏览器 返回 url uri text title 正常
百度浏览器 返回uri,text text包含url 正常
猎豹浏览器 无返回 提示不支持此类分享
搜狗浏览器 能调起来app,无acticion,无数据,无法分享 不支持,且无提示
gogle浏览器 只有一个text 里面只有url 文本
火狐浏览器 只有一个text 里面只有url 文本
魅族浏览器 只有一个text 里面只有url 文本
ViVo浏览器 只有一个text 里面只有url 文本
360浏览器 调不起来应用 不支持,且无提示