android1.5---2.1实现js调摄像头,调Java代码

Mybrowser代码
  1. package com.iame.mybrowser;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.graphics.Bitmap;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.KeyEvent;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.Window;  
  13. import android.view.View.OnClickListener;  
  14. import android.webkit.JsPromptResult;  
  15. import android.webkit.JsResult;  
  16. import android.webkit.URLUtil;  
  17. import android.webkit.WebChromeClient;  
  18. import android.webkit.WebView;  
  19. import android.webkit.WebViewClient;  
  20. import android.widget.Button;  
  21. import android.widget.EditText;  
  22. import android.widget.TextView;  
  23.   
  24.   
  25. public class MyBrowser extends Activity {  
  26.     private static final String DEBUG_TAG ="MyBrowser" ;  
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         //用title bar做进度条  
  31.         getWindow().requestFeature(Window.FEATURE_PROGRESS);  
  32.         setContentView(R.layout.main);  
  33.         //获取WebView  
  34.         final WebView webView=(WebView)findViewById(R.id.browser);  
  35.         //获取URL,也就是http://www.baidu.com这方面的地址  
  36.         final EditText text=(EditText)findViewById(R.id.url);  
  37.         //确定按钮  
  38.         Button go=(Button)findViewById(R.id.go);  
  39.         //设置支持javaScript  
  40.         webView.getSettings().setJavaScriptEnabled(true);  
  41.           
  42.           
  43.         //WebViewClient负责帮助WebView处理各种通知、请求事件等。想相当于程序嵌套了一个浏览器.浏览器里面的按钮支持事件,就需要WebView。  
  44.         webView.setWebViewClient(new WebViewClient(){  
  45.             //页面加载完成时要做的事件  
  46.             @Override  
  47.             public void onPageFinished(WebView view, String url) {  
  48.                   
  49.                   
  50.                 super.onPageFinished(view, url);  
  51.             }  
  52.             //页面加载时要做的事件  
  53.             @Override  
  54.             public void onPageStarted(WebView view, String url, Bitmap favicon) {  
  55.                 text.setText(url);  
  56.                   
  57.                 super.onPageStarted(view, url, favicon);  
  58.             }  
  59.         });  
  60.           
  61.         //WebChromeClient负责帮助WebView处理Javascript的对话框,网站图标,网站title,加载进度等。  
  62.         webView.setWebChromeClient(new WebChromeClient(){  
  63.               
  64.             //1 .弹出的对话框  
  65.             @Override  
  66.             public boolean onJsAlert(WebView view, String url, String message,final JsResult result) {  
  67.                   
  68.                 AlertDialog.Builder b2 = new AlertDialog.Builder(MyBrowser.this);  
  69.                 b2.setTitle(R.string.tite1);  
  70.                 b2.setMessage(message);  
  71.                 b2.setPositiveButton(R.string.ok,new AlertDialog.OnClickListener() {  
  72.                             public void onClick(DialogInterface dialog, int which) {  
  73.                                 //按确定后腰执行的代码,这句很重要  
  74.                                 result.confirm();  
  75.                                 //MyBrowser.this.finish();  
  76.                                   
  77.                             }  
  78.                         });  
  79.                   
  80.                 //设置对话框是否取消,默认是可以取消的。  
  81.                 //这种方式按还回键,齐作用  
  82.                 //b.setCancelable(true);  
  83.                 //这种方式按还回键,不齐作用  
  84.                 b2.setCancelable(false);  
  85.                 //创建对话框,才能显示  
  86.                 b2.create();  
  87.                 b2.show();  
  88.                 //如果还回的是flase将会多弹出窗口,不是我们想要的结果  
  89.                 return true;  
  90.             };  
  91.             //2 .对话框  
  92.             @Override  
  93.             public boolean onJsConfirm(WebView view, String url, String message,  
  94.                     final JsResult result) {  
  95.                 AlertDialog.Builder b3 = new AlertDialog.Builder(MyBrowser.this);  
  96.                 b3.setTitle(R.string.tite2);  
  97.                 b3.setMessage(message);  
  98.                 b3.setPositiveButton(R.string.ok,  
  99.                         new AlertDialog.OnClickListener() {  
  100.                             public void onClick(DialogInterface dialog, int which) {  
  101.                                   
  102.                                 result.confirm();  
  103.                                   
  104.                             }  
  105.                         });  
  106.                 b3.setNegativeButton(R.string.no,  
  107.                         new DialogInterface.OnClickListener() {  
  108.                             public void onClick(DialogInterface dialog, int which) {  
  109.                                 //按取消做的事件  
  110.                                   
  111.                                 result.cancel();  
  112.                                   
  113.                             }  
  114.                         });  
  115.                 b3.setCancelable(false);  
  116.                 b3.create();  
  117.                 b3.show();  
  118.                 return true;  
  119.             };  
  120.               
  121.             //3 .输入对话框  
  122.             @Override  
  123.             public boolean onJsPrompt(WebView view, String url, String message,  
  124.                     String defaultValue, final JsPromptResult result) {  
  125.                 //获得环境参数  
  126.                 final LayoutInflater factory = LayoutInflater.from(MyBrowser.this);  
  127.                 //导入预先设置的输入设置  
  128.                 final View v = factory.inflate(R.layout.javascript_prompt_dialog, null);  
  129.                 //获得输入框标题并设置输入框标题  
  130.                 ((TextView) v.findViewById(R.id.prompt_message_text)).setText(message);  
  131.                 //获得输入框平设置默认值  
  132.                 ((EditText) v.findViewById(R.id.prompt_input_field)).setText(defaultValue);  
  133.   
  134.                 AlertDialog.Builder b4 = new AlertDialog.Builder(MyBrowser.this);  
  135.                 b4.setTitle(R.string.tite3);  
  136.                 b4.setView(v);  
  137.                 b4.setPositiveButton(R.string.ok,  
  138.                         new AlertDialog.OnClickListener() {  
  139.                             public void onClick(DialogInterface dialog, int which) {  
  140.                                 String value = ((EditText) v.findViewById(R.id.prompt_input_field)).getText().toString();  
  141.                                 result.confirm(value);  
  142.                             }  
  143.                         });  
  144.                 b4.setNegativeButton(R.string.no,  
  145.                         new DialogInterface.OnClickListener() {  
  146.                             public void onClick(DialogInterface dialog, int which) {  
  147.                                 result.cancel();  
  148.                             }  
  149.                         });  
  150.                 b4.setOnCancelListener(new DialogInterface.OnCancelListener() {  
  151.                             public void onCancel(DialogInterface dialog) {  
  152.                                 result.cancel();  
  153.                             }  
  154.                         });  
  155.                   
  156.                 b4.show();  
  157.                 return true;  
  158.             };  
  159.   
  160.             //设置网页加载的进度条  
  161.             @Override  
  162.             public void onProgressChanged(WebView view, int newProgress) {  
  163.                 MyBrowser.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100 );  
  164.                   
  165.                 super.onProgressChanged(view, newProgress);  
  166.             }  
  167.             //设置应用程序的标题title  
  168.             @Override  
  169.             public void onReceivedTitle(WebView view, String title) {  
  170.                 MyBrowser.this.setTitle(title);  
  171.                 super.onReceivedTitle(view, title);  
  172.             }  
  173.         });  
  174.       
  175.           
  176.           
  177.         //确定搜索按钮  
  178.           
  179.         go.setOnClickListener(new OnClickListener(){  
  180.             public void onClick(View v)  
  181.             {  
  182.                 try   
  183.                 {  
  184.                     //取得编辑框中我们输入的内容  
  185.                     String url = text.getText().toString();  
  186.                     //判断输入的内容是不是网址  
  187.                     if ( URLUtil.isNetworkUrl(url) )  
  188.                     {    
  189.                         //装载网址  
  190.                         webView.loadUrl(url);  
  191.                     }  
  192.                     else  
  193.                     {  
  194.                         text.setText("输入网址错误,请重新输入" );  
  195.                     }  
  196.                 }  
  197.                 catch (Exception e)   
  198.                 {  
  199.                     Log.e(DEBUG_TAG, e.toString());  
  200.                 }  
  201.             }  
  202.         });  
  203.           
  204.         webView.loadUrl("file:///android_asset/index.html" );  
  205.     }  
  206.       
  207.       
  208.    //返回上一页  
  209.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  210.         WebView mWebView=(WebView)findViewById(R.id.browser);  
  211.         if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {  
  212.             mWebView.goBack();  
  213.             return true;  
  214.         }  
  215.         return super.onKeyDown(keyCode, event);  
  216.     }  
  217. }  
intdex.html

<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">

function onAlert(){
alert("This is a alert sample");
}
function onCamera(){
//var personaldata = window.PersonalData.getPersonalData();
var b=confirm("are you sure go to Camera?");
//alert("your choice is "+b);
}
function onPrompt33(){
var b1=prompt("please input your password","yangguangfu");
alert("your input is "+b1);
}


</script>
<pre>
<input type="button" value="Yang_Alert" onclick="onAlert()"/>
<input type="button" id="Personaldata" value="打开相机" onclick="onCamera()"/>
<input type="button" value="Yang_prompt" onclick="onPrompt33()"/>

<a href="http://www.google.com" />Google</a>
</pre>
</html>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iame.mybrowser" android:versionCode="1"
android:versionName="1.0.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="MyBroswer">

<activity android:label="MyBrowser" android:name="MyBrowser">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="3" /><!-- 版本标识 -->
<uses-permission android:name="android.permission.INTERNET"/><!-- 访问网络权限 -->
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 调用相机权限 -->
<uses-permission android:name="android.permission.CAMERA"/>
</manifest>

你可能感兴趣的:(java,android,String,url,dialog,button)