1. 程式人生 > >分享出去的連結,點選連結中的按鈕可喚醒app(web喚醒原生app)

分享出去的連結,點選連結中的按鈕可喚醒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