分享出去的链接,点击链接中的按钮可唤醒app(web唤醒原生app)

核心代码部分标注了红色!!!!!!!!!!!!!!!!

AndroidMianifest:

<activity android:name="com.palmnewsclient.test.OutsideActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <!-- 下面所设置的质需要和html端对调 -->
                <!-- 在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "sharetest://data/XXX"的链接 -->
                <data
                    android:host="com.newnet.ydc.palmNews"
                    android:scheme="scheme" />
            </intent-filter>
        </activity>

需要h5传过来的信息scheme://com.newnet.ydc.palmNews?newType=7&contentId=12345
注:scheme是协议,
com.newnet.ydc.palmNews是host值,后面的全是所需要的参数。


OutsideActivity类:

package com.palmnewsclient.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.newnet.ydc.palmNews.R;
import com.palmnewsclient.MainActivity;
import com.palmnewsclient.base.BaseActivity;
import com.palmnewsclient.bean.NewsListBean;
import com.palmnewsclient.data.AppConfig;
import com.palmnewsclient.http.rx.RxBus;
import com.palmnewsclient.http.subscriber.SimpleSubscriber;
import com.palmnewsclient.newcenter.OtherNewsTypeActivity;
import com.palmnewsclient.newcenter.VoteActivity;
import com.palmnewsclient.newcenter.helper.NewHelpUtils;
import com.palmnewsclient.usercenter.LoginActivity;
import com.palmnewsclient.utils.AppManager;
import com.palmnewsclient.utils.Constants;
import com.palmnewsclient.utils.SPUtils;

import static com.palmnewsclient.utils.SPUtils.getBooleanType;

public class OutsideActivity extends BaseActivity {

    private int newType;
    private int contentId;
    private String tooken;
    private TextView text;
    private String url;
    private NewsListBean.BodyEntity.DataEntity bean;
    @Override
    protected int initRootView() {
        return R.layout.activity_outside;
    }

    @Override
    protected void initTitle() {
        RxBus.getDefault().toObservable(OutSideBindSucceed.class).subscribe(new SimpleSubscriber<OutSideBindSucceed>() {
            @Override
            public void onNext(OutSideBindSucceed imageController) {
                if(imageController.isSuccess()) {
                    openActivity();
                }
            }
        });

        RxBus.getDefault().toObservable(OutSideEvent.class).subscribe(new SimpleSubscriber<OutSideEvent>() {
            @Override
            public void onNext(OutSideEvent event) {
                if(event.getOutside()) {
                    openActivity();
                }
            }
        });
    }

    @Override
    protected void initViews() {
        text = (TextView) findViewById(R.id.text);
    }

    @Override
    protected void initData() {
        String data = getIntent().getDataString();//接收到网页传过来的数据:scheme://com.newnet.ydc.palmNews?newType=7&contentId=12345
        if(data!=null) {
            String[] split = data.split(getPackageName()+"?");//以包名加问号为切割符来切割字符串
            String UrlData = split[1]; //就得到:newType=7&contentId=12345(这就是我们需要网页传给我们的数据)
            //以下是截取字符串处理拿到我们需要的参数值
            String[] param=UrlData.split("&");
            contentId=Integer.parseInt((param[0].split("="))[1]);
            newType=Integer.parseInt((param[1].split("="))[1]);
            Log.e("WEIWEI", "newType="+newType+"  contentId="+contentId);
            Log.e("WEIWEI", data);
        }else {
            text.setText("参数错误,无法启动");
            return;
        }
        boolean islogin = getBooleanType(this, Constants.USER_LOGIN_STATUS);
        boolean isBindMobile = getBooleanType(this, Constants.USER_LOGIN_THIRD_WAY_BIND_MOBILE_STATUS);
        boolean isthirdLogin=SPUtils.getBooleanType(this,Constants.USER_LOGIN_THIRD_WAY_STATUS);
        Log.e("WEI", "isLogin=="+islogin+"");
        if(islogin) {
            if(!isthirdLogin) {
                openActivity();
                Log.e("WEI", "登陆的状态下=========================");
            }else {
                if(isBindMobile) {
                    Log.e("WEI", "已绑定状态下=========================");
                    openActivity();
                }else {
                    Log.e("WEI", "未绑定状态下=========================");
                    Bundle b=new Bundle();
                    b.putCharSequence(AppConfig.SHOW_BIND_DIALOG,"bind");
                    AppManager.getInstance().jumpActivity(this, MainActivity.class, b);
                    AppManager.getInstance().finishActivity(this);

                }
            }

        }else{
            Log.e("WEI", "未登陆的状态下=========================");
            Bundle bundle=new Bundle();
            bundle.putString("OUT_SIDE","out_side");
            AppManager.getInstance().jumpActivity(this, LoginActivity.class, bundle);
            AppManager.getInstance().finishActivity(this);
        }
    }

    @Override
    protected void initListener() {
如果是第三方登录并且还未绑定
//        boolean thirdLoginStatus = SPUtils.getBooleanType(context, Constants.USER_LOGIN_THIRD_WAY_STATUS);
//        boolean loginStatus = SPUtils.getBooleanType(context, Constants.USER_LOGIN_STATUS);
//        boolean isBindMobile = SPUtils.getBooleanType(context, Constants.USER_LOGIN_THIRD_WAY_BIND_MOBILE_STATUS);
//        //7.8.12类型的文章必须有这步操作
//        boolean needLoginFunc = dataEntity.getNewType() == 7 || dataEntity.getNewType() == 8 || dataEntity.getNewType() == 12;
//        if(loginStatus&&thirdLoginStatus&&!isBindMobile&&needLoginFunc) {//第三方登录并且没有绑定手机,并且不是普通文章
//            //提示去绑定手机
//            Toast.makeText(context, "请先去绑定您的手机", Toast.LENGTH_SHORT).show();
//            Intent intent = new Intent(context, Main2Activity.class);
//            intent.putExtras(bundle);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startActivity(intent);
//        }else {
//            Intent intent = new Intent(context, webviewByNewsType);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.putExtras(bundle);
//            startActivity(intent);
//        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("WEI", "只走了onResume=========================");
    }

    private void openActivity() {
        getBundle();
        AppManager.getInstance().jumpActivity(this, MainActivity.class, null);
        if(newType==8) {
            AppManager.getInstance().jumpActivity(this, VoteActivity.class, bundle);
        }else {
            AppManager.getInstance().jumpActivity(this, OtherNewsTypeActivity.class, bundle);
        }
        AppManager.getInstance().finishActivity(this);
    }
    private Bundle bundle;
    @NonNull
    private void getBundle() {
        tooken= SPUtils.getStringType(this, Constants.USER_LOGIN_TOKEN);
        url= NewHelpUtils.getNewsUrlByNewsType(this,newType,contentId,tooken);
        Log.e("WEIWEI", url);
        bean=new NewsListBean.BodyEntity.DataEntity();
        bean.setTitle(NewHelpUtils.getNewsTitleByNewsType(newType));
        bean.setNewType(newType);
        bean.setContentId(contentId);
        bean.setId(contentId);
        Intent intent=new Intent();
        intent.putExtra(Constants.NEW_DETAIL_TITLE,NewHelpUtils.getNewsTitleByNewsType(newType));
        intent.putExtra(Constants.NEW_DETAIL_SHARE_BEAN,bean);
        intent.putExtra(Constants.NEW_DETAIL_URL,url);
        bundle=new Bundle();
        bundle.putString(Constants.NEW_DETAIL_URL, url);//路径
        bundle.putString(Constants.NEW_DETAIL_TITLE, NewHelpUtils.getNewsTitleByNewsType(newType));//文章详情标题
        bundle.putString(Constants.NEW_LINK_TITLE, NewHelpUtils.getNewsTitleByNewsType(newType));//文章链接标题
        bundle.putSerializable(Constants.NEW_DETAIL_SHARE_BEAN, bean);
    }


    @Override
    public void onClick(View v) {

    }
}

可以参考的博客: 点击打开链接 http://blog.csdn.net/daijin888888/article/details/50009387

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值