我还可以从.html文件的.java文件与Android的链接?文件、我还、链接、html

2023-09-06 15:07:00 作者:除了我,还有谁敢许你一生

我还可以从.HTML文件java文件,带链接的android系统的帮助?

Can I call .java file from .HTML file, with the help of link in android?

我是新的android应用开发。我也没有那么多的想法。所以,请帮助我。

I am new to android application development. And I have not that much idea. So Please Help me.

的.java文件的位置在src / my_package_name文件夹和.html文件的位置是在资产/ www文件夹。

Location of .java file is in src/my_package_name folder and location of .HTML file is in assets/www folder.

我想在HTML文件中的锚标记这个做的,我已经给 HREF =ContactUs.java链接。这是行不通的。

I am trying to do with this by anchor tag in html file, I have given href="ContactUs.java" to link. It is not working.

模拟器显示我的错误。这就是应用程序错误。所请求的文件没有被发现。 ContactUs.java(文件:///android_asset/ContactUs.java)

Emulator shows me error. That is Application Error. The Requested file was not found. ContactUs.java(file:///android_asset/ContactUs.java).

我不知道这是正确的方式与否。

I am not sure it is correct way or not.

所以,请给我建议,如果有什么更好的方式来做到这一点?

So please Suggest me if there is any better way to do this?

在此先感谢..

推荐答案

下面是从HTML打开活动的解决方案。

Here is a solution to open an activity from html.

首先包含您的WebView您的主要活动:

First your main activity containing your webview:

package com.test.jsitest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WVActivity extends Activity {

    private WebView m_wv = null;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wv);
        m_wv = (WebView) findViewById(R.id.webview);
        m_wv.getSettings().setJavaScriptEnabled(true);
        m_wv.loadUrl("file:///android_asset/www/index.html");
        m_wv.addJavascriptInterface(new ActivityLauncher(this), "Android");
    }
}

然后你的Javascript接口:

Then your Javascript Interface:

package com.test.jsitest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class ActivityLauncher {
    private Context m_context;

    public ActivityLauncher(Context context) {
        m_context = context;
    }

    public void launchActivity() {
        m_context.startActivity(new Intent((Activity)m_context,
             Activity2.class)); // Here you replace by your activity (ContactUs)
    }
}

和finaly您的HTML文件:

And finaly your html file:

<DOCTYPE html>
<html>
    <head>

    </head>
    <body>
        <a href="javascript:Android.launchActivity()">Link</a> 
    </body>
</html>

我测试了它和它的作品8级API之下。我希望它可以帮助你。

I tested it and it works under a level 8 API. I hope it helps you.