1. 程式人生 > >H5跳轉App的某個指定頁面

H5跳轉App的某個指定頁面

H5連結開啟原生App並跳轉到指定介面

因為目前專案需要先登入才能夠使用,所以跳轉時候需要判斷是否登入,先上圖,有圖有真相
app未啟動情況下,點選連結跳轉至MainActivity並傳遞引數id,但是判斷沒有登入就跳轉至LoginActivity,並保留要跳轉的介面id,為了登入成功後再跳轉
app未啟動情況下
app未啟動情況下,從登入介面登入成功跳轉至MainActivity,判斷是否有保留的跳轉的id,有的話就跳轉,此時activity棧中從下到上分別為MainActivity->OtherActivity
登入成功後跳轉
app登入情況下,點選跳轉連結,走OnNewIntent方法,直接跳轉至OtherActivity
登入情況下


具體步驟如下:
- 將清單檔案中首頁的exported屬性改為true
- 對首頁新增intent filter,設定scheme和host等屬性
- h5 window.location.href = scheme://host/pathPrefix
- 判斷登入邏輯
- 登入狀態下,singleTask,onNewIntent
- 未登入狀態,保留需要跳轉的介面id,並跳轉到登入介面,登入成功後在首頁獲取到要跳轉到id進行跳轉

清單檔案程式碼:

<?xml version="1.0" encoding="utf-8"?>
<manifest package
="com.hj.test" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl
="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:launchMode="singleTask" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:host="host" android:pathPrefix="/pathPrefix" android:scheme="scheme"/> </intent-filter> </activity> <activity android:name=".LoginActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".OtherActivity"/> </application> </manifest>

Application檔案程式碼:

package com.hj.test;

import android.app.Application;


public class App extends Application {

    private static boolean isLogin = false;

    private static int id = -1;//需要跳轉的acitivity的id

    @Override
    public void onCreate() {
        super.onCreate();
        isLogin = false;
        id = -1;
    }

    public static boolean isLogin() {
        return isLogin;
    }

    public static void setLogin(boolean islogin) {
        App.isLogin = islogin;
    }

    public static void setId(int id){
        App.id = id;
    }

    public static int getId(){
        return id;
    }
}

MainActivity檔案程式碼:

package com.hj.test;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(!App.isLogin()){//沒有登入就跳轉到登入介面
            App.setId(123);//快取需要跳轉的頁面,模擬id和OtherActivity對應
            finish();
            startActivity(new Intent(this,LoginActivity.class));
        }else{
            if(-1!=App.getId()){//登入成功後檢視是否有需要跳轉頁面的快取
                startActivity(new Intent(this,OtherActivity.class));
                App.setId(-1);//跳轉到頁面後,將快取刪掉
            }
        }

        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Uri uri = getIntent().getData();
        if(uri!=null){
            String id = uri.getQueryParameter("id");
            startActivity(new Intent(this,OtherActivity.class));
            App.setId(-1);//跳轉到頁面後,將快取刪掉
        }

    }

    /**
     * 首頁是singleTask的,在MainActivity已經初始化的情況下,h5跳轉進入MainActivity會觸發onNewIntent方法
     * @param intent
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Uri uri = intent.getData();
        if(uri!=null){
            String id = uri.getQueryParameter("id");
            startActivity(new Intent(this,OtherActivity.class));
            App.setId(-1);//跳轉到頁面後,將快取刪掉
        }
    }

}

LoginActivity檔案程式碼:

package com.hj.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!isTaskRoot()) {
            finish();
            return;
        }

        setContentView(R.layout.activity_login);
    }

    public void login(View view){
        App.setLogin(true);//設定登入成功
        finish();
        startActivity(new Intent(LoginActivity.this,MainActivity.class));
    }
}

OtherActivity檔案程式碼:

package com.hj.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class OtherActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
    }

}

Html5關鍵程式碼:

window.location.href="scheme://post/pathPrefix?id=123";//這裡id對應某一個需要跳轉的activity

github地址:點選這裡跳轉至github地址