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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package bb.pck;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class BBActivity extends Activity {
final Activity activity = this;
/**InterfaceJavascript */
public class JavaScriptInterface {
/**Procédure pour afficher des Toast*/
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
/**Utilisation des toasts */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
/** Procédure apellée quand on démarre l'application */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**Créer une vue Web pour afficher le contenu du site*/
WebView myWebView = (WebView) findViewById(R.id.webview);
/**Loader la page d'accueil */
myWebView.loadUrl("http://page");
/**Machine JavaScript pour utiliser le téléphone*/
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
/**Gestion de la navigation */
myWebView.setWebViewClient(new MyWebViewClient());
/**Activer le javascript du navigateur de l'application */
WebSettings WebSettings = myWebView.getSettings();
WebSettings.setJavaScriptEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Chargement en cours... " + progress + "%");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
}
/**Reprogrammer le WebViewClient pour gérer la navigation dans le site*/
/**Si l'utilisateur sort du site, on démarre l'activité du téléphone pour naviguer sur le web*/
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().equals("dom")){
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
} |
Partager