Activity跳转新方法(学习笔记)

1、Java 调用 js 里面的函数、效率并不是很高、估计要200ms左右吧、做交互性很强的事情、这种速度很难让人接受、而js去调Java的方法、速度很快、50ms左右、所以尽量用js调用Java方法
2、Java 调用 js 的函数、没有返回值、调用了就控制不到了
3、Js 调用 Java 的方法、返回值如果是字符串、你会发现这个字符串是 native 的、转成 locale 的才能正常使用、使用 toLocaleString() 函数就可以了、不过这个函数的速度并不快、转化的字符串如果很多、将会很耗费时间
4、Android4.2以下的系统存在着webview的js对象注入漏洞

所以出于这些原因,我们并未采用这种方式用于Native与webview交互,而是要介绍核武器—scheme,采用scheme 的方式。

scheme格式

先来个完整的URL Scheme协议格式:

xl://goods:8888/goodsDetail?goodsId=10011002
通过上面的路径 Scheme、Host、port、path、query全部包含,基本上平时使用路径就是这样子的。

  • xl代表该Scheme 协议名称
  • goods代表Scheme作用于哪个地址域
  • 8888代表该路径的端口号
  • goodsDetail代表Scheme指定的页面
  • goodsId代表传递的参数

举个栗子:

(该 URL 会调起车辆详情页):uumobile://mobile/carDetail?car_id=123456,其中 scheme 为 uumobile,host 为 mobile,relativePath 为 /carDetail,query 为 car_id=123456。
在什么场景使用?

下面介绍一下本人曾经常用的场景:

  • 其他应用想要调用你APP的某个页面
  • 自己的H5页面想要调用native的某个页面
  • 服务器下发路径,客户端根据服务器下发跳转路径跳转相应的页面
  • APP端收到服务器端下发的PUSH通知栏消息,根据消息的点击跳转路径跳转相关页面

scheme的使用

        <activity android:name=".activity.ProcessActivity">
            <intent-filter>
                <data
                    android:host="open.everystudy.app"
                    android:scheme="everystudy">
                </data>

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

                <action android:name="android.intent.action.VIEW"/>
            </intent-filter>
        </activity>

注意scheme、host名字随便取,协商好就行。
接下来看一下代码处理:

public class ProcessActivity extends AppCompatActivity {

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

    void distribute() {
        Uri uri = getIntent().getData();
        if (uri == null)
            finish();
        else {
            String path = uri.getPath();
            if (!TextUtils.isEmpty(path)) {
                jump(path, uri);
            }
            finish();
        }
    }

    void jump(String path, Uri uri) {
        switch (path) {
            case "/jump":
                String msg = uri.getQueryParameter("index");
                LogUtils.e(msg);
//                Intent intent = new Intent(this, JumpActivity.class);
//                intent.putExtra("index", msg);
//                startActivity(intent);
                JumpActivity_.intent(this).extra("index", msg).start();
        }
    }
}

跳转

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    //everystudy://open.everystudy.app/jump?index=jumptest
    @Click(R.id.btn_jump)
    void clickBtn() {
        String action = "everystudy://open.everystudy.app/jump?index=jumptest";
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(action));
        startActivity(intent);
    }
    @Click(R.id.btn_jump_web)
    void clickWebBtn() {
        Intent intent = new Intent(this, WebViewActivity.class);
        startActivity(intent);
    }
}
public class WebViewActivity extends AppCompatActivity {

    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        webView = (WebView) findViewById(R.id.webview);
        webView.loadUrl("file:///android_asset/" + "index.html");
    }
}
<html>

  <body style="background-color:yellow">
      <p style="font-size:20px;text-align:center;">
        <h1>web跳转Activity</h1>
      </p>

      <p style="font-size:20px;text-align:center;" >
        <a href="everystudy://open.everystudy.app/jump?index=jumptes">点击跳转</a>
      </p>
  </body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章