第三方浏览器分享链接到自己的应用

 一。清单文件相关配置,配置入口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浏览器    调不起来应用                                        不支持,且无提示

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值