1

Possible Duplicate:
Communication between Android Java and Phonegap Javascript?

I have some code I would like to run whenever the user presses a button within my Phonegap index.html interface

// toggle airplane mode

  Settings.System.putInt(
  context.getContentResolver(),
  Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Post an intent to reload

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);

I would like to launch that code whenever a button is pressed. Maybe make a javascript function somehow that runs java code (if possible), so I can make something like

    <input type="submit" onlick="reconnect();" value="Reconnect to Cell Tower">

I am using Cordova 1.9.0

Community
  • 1
  • 1
user1687621
  • 425
  • 1
  • 6
  • 14
  • just search for it http://stackoverflow.com/questions/2727763/communication-between-android-java-and-phonegap-javascript – Ken Sep 21 '12 at 02:34
  • I'm fairly new to android development. I do not have any knowledge of java. I also don't know if that would be compatible with 1.9.0. Certainly there is probably a simpler way to do this. – user1687621 Sep 21 '12 at 02:47
  • if that solution is complicated for you you have to rebuild your know-how because the answer to that question is really easy to apply for a Java programmer, it's also fairly easy in its own concept. – Ken Sep 21 '12 at 02:50

1 Answers1

1

Just implement corrodovaInterface in your main activity like this..

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...
}

you may need to import

import org.apache.cordova.*;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.IPlugin;

Thanks.

Nikhil Dinesh
  • 3,359
  • 2
  • 38
  • 41