Possible Duplicate:
Communication between Android Java and Phonegap Javascript?
我有一些代码想要在用户按下 Phonegap index.html 界面中的按钮时运行
//切换飞行模式
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
//发布重新加载的 Intent
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
我想在按下按钮时启动该代码。也许以某种方式制作一个运行 java 代码的 javascript 函数(如果可能),这样我就可以制作类似的东西
<input type="submit" onlick="reconnect();" value="Reconnect to Cell Tower">
我正在使用 Cordova 1.9.0
只需像这样在您的主要 Activity 中实现 corrodovaInterface..
public class MainActivity extends Activity implements CordovaInterface, OnClickListener {
/** Called when the activity is first created. */
CordovaWebView cwv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cwv = (CordovaWebView) findViewById(R.id.webContent);
cwv.loadUrl("your index file");
Button btnSearch = (Button)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(this);
}
public void onClick(View v) {
//your code goes here...
}
你可能需要导入
import org.apache.cordova.*;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.IPlugin;
谢谢。