1、前言
本demo实现的功能是接收第三方应用的系统分享内容,并显示出来,先看看效果图:
(左图为分享页面,右图为接收分享页面)
2、实现原理
通过Intent获取到第三传过来的分享内容,一般是文字描述加链接,获取其中的文字,按自己想要的方式显示出来即可,然后通过其中的地址链接,去获取网页源码,解析其中的图片地址,拿到图片地址将其显示到imageview即可,有些网站无法获取完整源码或拿不到图片地址,则直接取网站的favicon.ico。
3、代码实现
1、在对应的activity中注册拦截:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image
private String filterSpecialStr(String regex, String data) {
//sb存放正则匹配的结果
StringBuilder sb = new StringBuilder();
//编译正则字符串
Pattern p = Pattern.compile(regex);
//利用正则去匹配
Matcher matcher = p.matcher(data);
//如果找到了我们正则里要的东西
while (matcher.find()) {
//保存到sb中,"\r\n"表示找到一个放一行,就是换行
sb.append(matcher.group()).append("\r\n");
}
return sb.toString();
}
拿到内容中的地址后,用Jsoup去获取该网页的源码,然后去源码中拿图片地址:
public static List<String> getImgSrcListFromHtml(Document document, String url) throws MalformedURLException {
List<String> list = new ArrayList<>();
//获取目标
// Document document = Jsoup.parse(html);
Elements elements = document.select("img");//img.lazy
Elements elements2 = elements.select("img.lazy");//img.lazy
//先找lazy标签,正文配图一般用此标签
int len = elements2.size();
for (int i = 0; i < len; i++) {
list.add(elements2.get(i).attr("data-original"));
}
//找不到再找img标签
if (list.size() == 0) {
int size = elements.size();
for (int i = 0; i < size; i++) {
String u = elements.get(i).attr("src");
if (!u.startsWith("http")) {
u = "http:" + u;
}
if (u.endsWith(".jpg")) {
list.clear();
list.add(u);
return list;
}
list.add(u);
}
}
//实在找不到就用icon
if (list.size() == 0) {
list.add(getIconUrlString(url));
}
return list;
}
拿到图片地址之后想干嘛就干嘛了。
4、完整代码
public class MainActivity extends AppCompatActivity {
private TextView tvTitle;
private TextView tvContent;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTitle = findViewById(R.id.tv_title);
imageView = findViewById(R.id.image);
tvContent = findViewById(R.id.tv_content);
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);
String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
String sUrl = filterSpecialStr(regex, share);
String[] aUrl = sUrl.split("http");
if (aUrl.length > 2) {
sUrl = "http" + aUrl[2];
}
final String url = sUrl;
String[] str;
if (TextUtils.isEmpty(title)) {
assert share != null;
str = share.split(url);
if (str.length > 0) {
title = str[0];
}
}
tvTitle.setText(title);
tvContent.setText(share);
new Thread(new Runnable() {
@Override
public void run() {
try {
final List<String> list = getImgSrcListFromHtml(Jsoup.connect(url).get(), url);//Jsoup.connect(url).get()
if (list.size() == 0) {
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.with(MainActivity.this)
.load(list.get(0))
.into(imageView);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
void dealPicStream(Intent intent) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
}
void dealMultiplePicStream(Intent intent) {
ArrayList<Uri> arrayList = intent.getParcelableArrayListExtra(intent.EXTRA_STREAM);
}
public static List<String> getImgSrcListFromHtml(Document document, String url) throws MalformedURLException {
List<String> list = new ArrayList<>();
//获取目标
// Document document = Jsoup.parse(html);
Elements elements = document.select("img");//img.lazy
Elements elements2 = elements.select("img.lazy");//img.lazy
//先找lazy标签,正文配图一般用此标签
int len = elements2.size();
for (int i = 0; i < len; i++) {
list.add(elements2.get(i).attr("data-original"));
}
//找不到再找img标签
if (list.size() == 0) {
int size = elements.size();
for (int i = 0; i < size; i++) {
String u = elements.get(i).attr("src");
if (!u.startsWith("http")) {
u = "http:" + u;
}
if (u.endsWith(".jpg")) {
list.clear();
list.add(u);
return list;
}
list.add(u);
}
}
//实在找不到就用icon
if (list.size() == 0) {
list.add(getIconUrlString(url));
}
return list;
}
private String filterSpecialStr(String regex, String data) {
//存放正则匹配的结果
StringBuilder sb = new StringBuilder();
//编译正则字符串
Pattern p = Pattern.compile(regex);
//利用正则去匹配
Matcher matcher = p.matcher(data);
//如果找到了我们正则里要的东西
while (matcher.find()) {
//保存到sb中,"\r\n"表示找到一个放一行,就是换行
sb.append(matcher.group()).append("\r\n");
}
return sb.toString();
}
// 获取Icon地址
public static String getIconUrlString(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
return url.getProtocol() + "://" + url.getHost() + "/favicon.ico";
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="100dp" android:layout_margin="20dp" android:background="@color/cardview_light_background" tools:ignore="MissingConstraints">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="6dp" android:orientation="vertical">
<TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxEms="20" android:singleLine="true" android:text="Hello World!" android:textColor="#000000" android:textSize="18sp" />
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="4dp">
<TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:maxLines="3" android:padding="3dp" android:text="Hello World!" />
<ImageView android:id="@+id/image" android:layout_width="60dp" android:layout_height="60dp" android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
5、项目下载地址
项目下载地址