2011-09-03 43 views
8
調用Android的活動

我是新來的PhoneGap,我能夠實現與PhoneGap的基本應用,現在進一步提升的話,我想的PhoneGap與Android活動連接,基本上我打算什麼叫使用javascript函數的startActivity()方法。如何從PhoneGap的

我試圖Communication between Android Java and Phonegap Javascript?

,但我沒有打電話的活動,導致強制關閉錯誤。 幫幫我,等待回覆!

+1

請發佈錯誤日誌。 –

回答

25

,而無需使用任何插件作爲下列任何Java本機代碼調用調用。

請按以下步驟操作。

  1. 將下面的代碼替換爲您現有的DroidGap活動。

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        super.init(); // Calling this is necessary to make this work 
        appView.addJavascriptInterface(this, "MainActivity"); 
    
        /* "this" points the to the object of the current activity. "MainActivity" is used to refer "this" object in JavaScript as in Step 3. */ 
    
        super.loadUrl("file:///android_asset/www/index.html"); 
    } 
    
  2. 在當前(本)活動添加自定義函數如下。

    public void customFunctionCalled() { 
        Log.e("Custom Function Called", "Custom Function Called"); 
    } 
    
  3. 現在,從您的HTML/JavaScript代碼中調用該函數,如下所示。

    <script type="text/javascript"> 
        function callNewActivity() { 
         window.MainActivity.customFunctionCalled(); 
        } 
    </script> 
    

這將在MainActivity調用customFunctionCalled()

測試環境 日食 - 3.7.2 Android 2.2的模擬器 的PhoneGap - 2.0.0

請提供您的意見在這裏,以提高博客張貼。 http://phonegapexplorers.blogspot.in/2012/08/call-native-java-code-phonegap-android.html

+0

我用了很長時間。但最近有一些與Android 4.2.2的問題。從網上學習,它可以正常使用sdk> = 17。您能否告訴我需要使用sdk> = 17的更改? – AtanuCSE

+0

@AvtarSingh Suchariya Hy我跟着你的教程,但我得到:07-08 23:44:04.845:E/Web控制檯(25296):Uncaught TypeError:對象[對象對象]沒有方法'customFunctionCalled'at file:/// android_asset/www/js/index.js:31 這是爲什麼?我聽說如果目標SDK是17或更多(我認爲它是我的情況),必須進行一些修改。但我不知道那是什麼。你能幫我麼? –

+0

@AtanuCSE hy man你有沒有想過使用sdk> = 17的腳本?你能告訴我該怎麼做嗎? –

2

它很難不知道你想要做什麼,但是寫一個插件的道路可能是一條可行的路。查看;

http://smus.com/android-phonegap-plugins

這個插件可以爲你工作原樣,還是給你如何在本做自己良好的指針。

0

我嘗試你之前嘗試做的事情,將phonegap更新到2.0.0版本並向上,最好的方法是使用插件。這是資產文件夾內電話號碼上的js。確保構造id爲「nativecall」的div元素和一個樣本按鈕來檢測它。確保觀察LogCat來檢查錯誤消息。

window.echo = function(str, callback) { 
    cordova.exec(callback, function(err) { 
     callback('Nothing to echo.'); 
    }, "Echo", "echo", [str]); 
}; 

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicity call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
    }, 
    // Update DOM on a Received Event 
    receivedEvent: function() { 

     var abiter = $('#nativecall').html(); 

      $("#abutton").click(function() { 
        window.echo(abiter, function(echoValue) { 
        alert(echoValue = abiter); // should alert true. 
       }); 
      }); 


    } 
}; 

app.initialize(); 

on src添加服務名稱爲「Echo」的新類方法。

package org.apache.cordova.plugin; 

import org.apache.cordova.api.CallbackContext; 
import org.apache.cordova.plugin.AndroidActivities; 
import org.apache.cordova.api.CordovaPlugin; 
import org.json.JSONArray; 
import org.json.JSONException; 

/** 
* This class echoes a string called from JavaScript. 
*/ 
public class Echo extends CordovaPlugin { 
    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     if (action.equals("echo")) { 
      String message = args.getString(0); 
      new AndroidPublicFunction(message); //call function from AndroidActivities 
      this.echo(message, callbackContext); 
      return true; 
     } 
     return false; 
    } 

    private void echo(String message, CallbackContext callbackContext) { 
     if (message != null && message.length() > 0) { 
      callbackContext.success(message); 
     } else { 
      callbackContext.error("Expected one non-empty string argument."); 
     } 
    } 
}