zoukankan      html  css  js  c++  java
  • Android 网页打开App进入对应页面

    ### 场景
    产品中有个功能是分享,用户点击分享的链接可打开app(若没有下载,跳转应用宝下载),进入相应的页面。
    ### 代码
    一般会在闪屏页添加以下代码(程序的入口)
    在```AndroidManifest.xml```中
    ```
    <activity
    android:name=".mvvm.splash.adsplash.AdSplashActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    =======这里是添加的代码 start======
    <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:scheme="ylbb"/>
    </intent-filter>
    =======这里是添加的代码 end=======
    </activity>
    ```
    接下来,在AdSplashActivity中接收并处理传过来的参数,在onCreate中。
    ```
    class AdSplashActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    // 网页打开app 进入测评 详情
    if (intent.data.checkNotNull()) {
    var productId = intent.data.getQuery()
    if (productId.checkNotNull()) {
    //这里写逻辑处理
    }
    }
    }
    }
    ```
    跟h5协商好对应的参数字段
    ```
    <html>
    <meta charset="UTF-8">
    <body>
    <h1>Test Scheme</h1> <!--自动加载隐藏页面跳转-->
    <!--手动点击跳转-->
    <a href="ylbb://ceping:productId?666">点击打开APP并将值传过去</a>
    </body>
    </html>
    ```
    ### 补充
    + 若网页定义为:```ylbb://ylbb.com:80/params?p=6&id=7```
    ```
    Intent intent = getIntent();
    String scheme = intent.getScheme();// ylbb
    Uri uri = intent.getData();

    if (uri != null) {
    String host = uri.getHost();//ylbb.com
    String dataString = intent.getDataString();//ylbb://ylbb.com:80/params?p=6&id=7
    String id = uri.getQueryParameter("id");//7
    String path = uri.getPath();///params
    String path1 = uri.getEncodedPath();//params
    String queryString = uri.getQuery();//p=6&id=7
    }
    ```
    + 若```getIntent.getData()```为```null```,可能是在其他```Activity```添加的```scheme```
    + 一般添加在闪屏页(app启动一些初始化操作都在这里进行),但是接收到数据可暂时保存下来,在闪屏页过后再去处理(如:```MainActivity```)

    参考链接:
    + [Android 通过网页打开自己的APP(scheme)](https://blog.csdn.net/qduningning/article/details/37602101)

  • 相关阅读:
    一个体验好的Windows 任务栏缩略图开发心得
    扫脸动画
    ShimmerTextView
    201512-2 消除类游戏 (水题,暴力)
    CCF 201512-1 数位之和 (水题)
    UVa 557 Burger (概率+递推)
    CCF 201604-2 俄罗斯方块 (模拟)
    CCF 201604-1 折点计数 (水题,暴力)
    UVa 10213 How Many Pieces of Land ? (计算几何+大数)
    UVa 1641 ASCII Area (计算几何,水题)
  • 原文地址:https://www.cnblogs.com/merbn/p/10685952.html
Copyright © 2011-2022 走看看