0

下面是我的代碼,但它不工作時,誤差函數調用僅如何調用本地警告框,同時使用PhoneGap的

本地插件

package com.gami.fre; 

import org.apache.cordova.api.Plugin; 
import org.apache.cordova.api.PluginResult; 
import org.json.JSONArray; 


import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.util.Log; 
import android.widget.Toast; 


public class ConfirmBox extends Plugin { 

    public static final String NATIVE_ACTION_STRING="nativeAction";  
    public static final String SUCCESS_PARAMETER="success"; 
    public Context context; 
    public int result=0; 

    @Override 
    public PluginResult execute(String action, JSONArray data, String callbackId) 
    { 

      Log.d("HelloPlugin", "PhoneGap/Cordova!");    
      //only perform the action if it is the one that should be invoked 


      if (NATIVE_ACTION_STRING.equals(action)) 
      {     
       String resultType = null;      

       try {       
        resultType = data.getString(0);     
        }      
       catch (Exception ex) { 

        Log.d("HelloPlugin", ex.toString());   
        }      

       if (resultType.equals(SUCCESS_PARAMETER)) 
       {   
        Log.d("hisu", resultType); 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
          context); 

         // set title 
         //alertDialogBuilder.setTitle("Your Title"); 

         // set dialog message 
         alertDialogBuilder 
          .setMessage("Are you sure want to Exit!") 
          .setCancelable(false) 
          .setPositiveButton("Yes",new DialogInterface.OnClickListener() 
          { 
           public void onClick(DialogInterface dialog,int id) 
           { 
            //ConfirmBox.this.finish(); 
           } 
           }) 
          .setNegativeButton("No",new DialogInterface.OnClickListener() 
          { 
           public void onClick(DialogInterface dialog,int id) 
           { 

            dialog.cancel(); 
           } 
          }); 

          // create alert dialog 
          AlertDialog alertDialog = alertDialogBuilder.create(); 
          // show it 
          alertDialog.show(); 
return new PluginResult(PluginResult.Status.OK, result); 

       }  
      else 
      {      
        return new PluginResult(PluginResult.Status.ERROR, "Oops, Error :(");     
      }   
     } 

    return null; 

    } 

} 

電話差距通話

<script type="text/javascript" > 
function callNativePlugin(returnSuccess) 
{  
Helo.callNativeFunction(nativePluginResultHandler, nativePluginErrorHandler, returnSuccess); 

} 

function nativePluginResultHandler (result) 
{  
    //alert("SUCCESS: \r\n"+result); 

} 

function nativePluginErrorHandler (error) 
{  

alert("ERROR: \r\n"+error); 

} 

只有錯誤函數每次調用;成功調查正在發生,但在第一行後立即跳過。即在日誌打印其從功能和顯示錯誤跳過:NULL

請幫助解決這個

回答

0

希望,addJavascriptInterface()將幫助你從JavaScript調用Android原生功能

例:

原生代碼:webview.addJavascriptInterface(this.Activity,「SomeKey」);

的Javascript:

在這裏,你可以調用本機的功能

window.SomeKey.execute() //執行的是原生功能

+0

感謝,但實際需要的插件 – Rick 2013-02-15 18:36:49

+0

是用Java編寫或JavaScript您的插件代碼? – PavanBhushan 2013-02-16 11:52:33

+0

謝謝,我的代碼是用Java編寫的 – Rick 2013-02-18 07:59:45

0

我今年早些時候創建了這個。我不太確定它是否仍然適用於當前的Phonegap。這是我創建的一個插件,允許您創建一個AlertList並返回用戶的選擇。

https://github.com/kidino/phonegap-alertdialoglist-plugin

Example of the Android AlertList Dialog for Phonegap

一般情況下,你需要在你的JavaScript做的是創建一個數組。第一項將成爲AlertList的標題。然後調用showlist()函數並傳遞數組作爲參數。查看repo中www文件夾中的示例。

<script> 
    var fruitlist = [ 
         "The Fruit List Title", // this is the title 
         "Orange", 
         "Apple", 
         "Watermelon", 
         "Papaya", 
         "Banana", 
         "Pear" 
       ]; 

    function showlist(thelist) { 
      cordova.exec(
        function(listitem) { 
          alert("You selected "+ thelist[listitem]); 
        }, 
        function(error) { 
          alert("Error Occured"); 
        }, "AlertList", "alertlist", thelist); 
    } 
</script> 

在你的HTML,你可以有這樣的:

<h1><a href="javascript:showlist(fruitlist)">FRUITS</a></h1> 
相關問題