October 29, 2011

PhoneGap(JavaScript)からAndroidのメソッドを実行する

参考:
http://blog.livedoor.jp/hiroki0907/archives/51728780.html

--Java(Android)--
package com.phonegap.example;

import android.os.Bundle;
import android.widget.Toast;

import com.phonegap.DroidGap;

public class XxxActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        appView.addJavascriptInterface(new JavaScriptCallback(), "android");
        super.loadUrl("file:///android_asset/www/index.html");
    }

    public class JavaScriptCallback {
        public void callbackTest(String str) {
            Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
        }
    }
}

--JavaScript--
function callback_test() {
    android.callbackTest("yyy");
}

No comments:

Post a Comment