2016-09-23 6 views
3

私のJavaアプリをwebviewのコンテンツと通信しようとしています。アンドロイドのJavascriptブリッジを使用する

詳しくは、ボタンをクリックするだけでトーストを表示することができますか?

Googleで検索していくつかの調査を行ったところ、次のコードになりました。

P.S: - Androidスタジオを使用していますが、私は物事を完了するためにinorderをコンパイルする必要がある外部ライブラリがありますか?または、他の何か ?続き

は私WebviewActivityコードです: -

public class WebviewActivity extends AppCompatActivity { 

private static final String TAG = "Main"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_webview); 
    //WebView Object 
    WebView browser; 
    browser=(WebView)findViewById(R.id.webView); 
    //Enable Javascript 
    browser.getSettings().setJavaScriptEnabled(true); 
    //Inject WebAppInterface methods into Web page by having Interface 'Android' 
    browser.addJavascriptInterface(new WebAppInterface(this), "Android"); 
    browser.loadUrl("http://www.somewebsite.com/app/form.html"); 
} 
//Class to be injected in Web page 
public class WebAppInterface { 
    Context mContext; 

    /** Instantiate the interface and set the context */ 
    WebAppInterface(Context c) { 
     mContext = c; 
    } 

    /** 
    * Show Toast Message 
    * @param toast 
    */ 
    public void showToast(String toast) { 
     Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); 
    } 

    /** 
    * Show Dialog 
    * @param dialogMsg 
    */ 
    public void showDialog(String dialogMsg){ 
     AlertDialog alertDialog = new AlertDialog.Builder(mContext).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle("JS triggered Dialog"); 

     // Setting Dialog Message 
     alertDialog.setMessage(dialogMsg); 

     // Setting alert dialog icon 
     //alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); 

     // Setting OK Button 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Toast.makeText(mContext, "Dialog dismissed!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 

    /** 
    * Intent - Move to next screen 
    */ 
    public void moveToNextScreen(){ 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 
     // Setting Dialog Title 
     alertDialog.setTitle("Alert"); 
     // Setting Dialog Message 
     alertDialog.setMessage("Are you sure you want to leave to next screen?"); 
     // Setting Positive "Yes" Button 
     alertDialog.setPositiveButton("YES", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         //Move to Next screen 

        } 
       }); 
     // Setting Negative "NO" Button 
     alertDialog.setNegativeButton("NO", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // Cancel Dialog 
         dialog.cancel(); 
        } 
       }); 
     // Showing Alert Message 
     alertDialog.show(); 
    } 
    } 
} 

form.html: -

<html> 
<head> 
    <style> 
    body{ 
background-color: #FA5858; 
color:#fff; 
    } 
input{ 
background-color: #F7D358; 
width: 300px; 
padding:10px; 
color: #000; 
} 
div#content{ 
padding:20px; 
background-color: #F7D358; 
    color: #000; 
} 
</style> 
<script type="text/javascript"> 
    function showAndroidToast(toastmsg) { 
    Android.showToast(toastmsg); 
    } 
function showAndroidDialog(dialogmsg) { 
     Android.showDialog(dialogmsg); 
    } 
    function moveToScreenTwo() { 
    Android.moveToNextScreen(); 
    } 
</script> 
    </head> 
    <body> 
    <center> 
    <h3>Binding JavaScript code to Android code</h3> 
     <div id="content"> 
//some content here 
     </div> 
    <div> 
    Here are few examples: 
    </div> 
    <div> 
     <input type="button" value="Make Toast" onClick="showAndroidToast('Toast made by Javascript')" /><br/> 
     <input type="button" value="Trigger Dialog" onClick="showAndroidDialog('This dialog is triggered by Javascript ')" /><br/> 
     <input type="button" value="Take me to Next Screen" onClick="moveToScreenTwo()" /> 
      </div> 
     </center> 
    </body> 
    </html> 

私はこの上記のコードを持っているが、それはアンドロイド・スタジオで働いていないので、すべてのposiblitiesを試してみましたついにここに私の質問を掲載しました。このため

私の研究: -

https://developer.android.com/guide/webapps/webview.html 
https://developer.android.com/reference/android/webkit/WebView.html 
http://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview 

答えて

2

WebAppInterfaceクラス内のあなたの方法は@JavascriptInterface注釈が欠落しています。

あなたWebAppInterfaceクラスは、このようにする必要があり、

public class WebAppInterface { 
    Context mContext; 

    WebAppInterface(Context c) { 
     mContext = c; 
    } 

    @JavascriptInterface 
    public void showToast(String toast) { 
    } 

    @JavascriptInterface 
    public void showDialog(String dialogMsg){ 
    } 

    @JavascriptInterface 
    public void moveToNextScreen(){ 
    } 

} 

編集のWebViewにWebコンテンツをロードするとき

API17にアプリを制限することを忘れないでください+、そうでない場合は、あなたのアプリケーションが脆弱になります攻撃する。下の@ Robertのコメントを読んでください。

+0

あなたはちょうど私の日を救った! 、愚かな間違いがある..スムーズに作業! 。私を助けてくれてありがとう。 – Vikrant

+0

うれしい私は助けることができます。幸運:) –

+1

@Vikrant WebViewにWebコンテンツを読み込む際に、あなたのアプリをAPI17 +に制限することを忘れないでください。さもなければ、あなたのアプリケーションは攻撃に対して脆弱になります。 – Robert

関連する問題