Topic
3 replies Latest Post - ‏2012-10-08T19:58:44Z by IshitaJain
IshitaJain
IshitaJain
20 Posts
ACCEPTED ANSWER

Pinned topic Invoke Client Javascript method from android whenever location change

‏2012-10-05T00:01:28Z |
Android
So, I am trying to invoke a method in a sql adapter and http adapter from my android native page.
But, I have no idea how to do that
I know how to invoke adapter procedures from the client side java script files but again how do i invoke these functions in java script from native page.

Basically, my application, I want to send latitude and longitude from NATIVE PAGE (android activity) to the web part of this application whenever the location is changes.

I used finish() but that works only once, it does not executes everytime the location is changed.
What should I do, please HELP!
  • IshitaJain
    IshitaJain
    20 Posts
    ACCEPTED ANSWER

    Re: Invoke Client Javascript method from android whenever location change

    ‏2012-10-05T00:04:43Z  in response to IshitaJain
    Once again, this is my code in native page
    public class HelloNative extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String name = getIntent().getStringExtra("nameParam");
    Toast.makeText( getApplicationContext(),name ,Toast.LENGTH_SHORT ).show();
    getLocation();

    }
    public void getLocation(){
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    }
    public class MyLocationListener implements LocationListener

    {
    @Override

    public void onLocationChanged(Location loc){
    String latitude = Double.toString(loc.getLatitude());
    String longitude = Double.toString(loc.getLongitude());
    Intent data = new Intent();
    data.putExtra("Latitude", latitude);
    data.putExtra("Longitude", longitude);
    setResult(RESULT_OK, data);
    finish();

    }
    }

    Here it, does send the latitude and longitude to my web page but just once.
    I want this to be sent to my web page every time the location changes.
    Can SOMEBODY PLEASE HELP ME WITH THIS?
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: Invoke Client Javascript method from android whenever location change

      ‏2012-10-05T04:44:42Z  in response to IshitaJain
      We currently don't have a Worklight native API for Android, I believe we'll have one in the future.

      Here's something you can do:

      
      URL getCustomersUrl = 
      
      new URL(
      "http://localhost:8080/invoke?adapter=customers&procedure=getCustomers&parameters=[]"); URLConnection getCustomersConnection = getCustomersUrl.openConnection(); BufferedReader in = 
      
      new BufferedReader(
      
      new InputStreamReader(getCustomersConnection.getInputStream())); String inputLine;   
      
      while ((inputLine = in.readLine()) != 
      
      null)
      { System.out.println(inputLine); 
      }   in.close();
      


      localhost -> Worklight Server host
      8080 -> Worklight Server port
      customers -> Adapter name
      getCustomers -> Procedure name
      [] -> Parameters you want to pass to the procedure

      Here's an article on JSON (you get JSON via strings back) on Android:
      http://www.vogella.com/articles/AndroidJSON/article.html

      "I want this to be sent to my web page every time the location changes."

      You may want to get the location using Java/Android. Here's a useful link to get you started:
      http://stackoverflow.com/a/8543819/186909
      • IshitaJain
        IshitaJain
        20 Posts
        ACCEPTED ANSWER

        Re: Invoke Client Javascript method from android whenever location change

        ‏2012-10-08T19:58:44Z  in response to SystemAdmin
        Thank you very much Sir, It really helped and it works fine