1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.gallifrey.webviewtest;
import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent; import android.os.Bundle; import android.webkit.JavascriptInterface; import android.webkit.WebSettings; import android.webkit.WebView;
public class MainActivity extends AppCompatActivity { private WebView webView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
webView=findViewById(R.id.web_view);
WebSettings webSettings=webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); webView.addJavascriptInterface(this,"android"); webView.loadUrl("file:///android_asset/test.html");
}
@JavascriptInterface public void getUserName(String username){ String temp=username; Intent data=new Intent(this,SecondActivity.class); data.putExtra("user",temp); startActivity(data); }
}
|