ref: https://stackoverflow.com/questions/19426742/dont-navigate-to-other-pages-in-webview-disable-links-and-references
WebView gives developers an easy way to embed a web-based application to an Android app with almost no code. Just give it some URL and it works.
Implementation of the application which displays the predefined web page in a WebView is relatively straightforward.
myWebView.loadUrl("your_pages");
There are two scenarios how a web app might react to a user click event. The first one is redirection. WebView client has quite an easy way to listen for redirects. You just need to override its method shouldOverrideUrlLoading()
.
package com.demilion.myapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import java.io.IOException;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.webkit.WebViewClient;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends Activity {
private AdView mAdView;
private boolean loaded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,
"ca-app-pub-000000000000000~0000000000");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
WebView myWebView = (WebView) this.findViewById(R.id.webView);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.addJavascriptInterface(new AudioInterface(this), "AndAud");
// Enable JavaScript
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//myWebView.loadUrl("https://codepen.io/loktar00/pen/CHpGo/");
//myWebView.loadUrl("file:///android_asset/myhtml.html");
myWebView.loadUrl("https://codepen.io/loktar00/pen/CHpGo/");
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(loaded == false){
view.loadUrl(url);
loaded = true;
return true;
}else{
return false;
}
}
});
}
public class WebAppInterface {
Context context;
/**
* Instantiate the interface and set the context
*
* @param context
*/
public WebAppInterface(Context context) {
this.context = context;
}
/**
* Show a dialog from the web page.
*
* @param message
* message of the dialog
*/
@JavascriptInterface
public void showDialog(String message) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
builder.setMessage(message).setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
// Create the AlertDialog object and return it
builder.create().show();
}
}
public class AudioInterface {
Context mContext;
AudioInterface(Context c) {
mContext = c;
}
//Play an audio file from the webpage
@JavascriptInterface
public void playAudio(String aud) { //String aud - file name passed
//from the JavaScript function
final MediaPlayer mp;
try {
AssetFileDescriptor fileDescriptor =
mContext.getAssets().openFd(aud);
mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
fileDescriptor.close();
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
No hay comentarios:
Publicar un comentario