android - 如何在 phonegap/cordova 中运行 java 代码?

标签 android cordova

<分区>

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;

谢谢。

关于android - 如何在 phonegap/cordova 中运行 java 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12523483/

相关文章:

android - 如何将 SQLite 数据库从 Android 复制到 MySQL 数据库(复制/同步)

android - 使用自定义字体更改 Tablayout 的选定选项卡颜色

javascript - Firebase - getRedirectResult 在 Cordova 浏览器中不起作用

cordova - Android 无法构建 - 找不到 com.android.tools :common:25. 5.0-alpha-preview-02

css - 根据设备方向更改图像

android - 处理 “javax”错误

c# - 如何在 Xamarin 中压缩来自 PCL 的视频

java - 如何在应用程序首次启动时运行特定代码?

java - Android - 位置管理器 requestLocationUpdates 瓶颈

cordova - 安卓开发者工具 : exclude asset files (php) from apk build