简体   繁体   中英

How to make call to PHP file in background when Transition happen (ENTER & EXIT ) cordova geofence plugin in background?

I want to make call to the php file which is located in server when transition happen (ENTER & EXIT ) in background only. I read the cordova geofence plugin documentation for that I need to create TransitionReceiver.java file to achieve this.

I read this native implementation

Any help

I tried below code:

package com.cowbell.cordova.geofence;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.os.AsyncTask;
import com.loopj.android.http.*;
import java.util.*;


public class TransitionReceiver extends BroadcastReceiver {

 @Override
  public void onReceive(Context context, Intent intent) {
    Logger.setLogger(new Logger(GeofencePlugin.TAG, context, false));
    Logger logger = Logger.getLogger();

    String error = intent.getStringExtra("error");

    if (error != null) {
        //handle error
        logger.log(Log.DEBUG, error);
    } else {
        String geofencesJson = intent.getStringExtra("transitionData");                
        PostLocationTask task = new TransitionReceiver.PostLocationTask();           
        task.execute(geofencesJson);                       
    }       
}

private class PostLocationTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... geofencesJson) {
        try {
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams data=new RequestParams();
            params.put("key1","value1");
            String url="someurl/xyz.php";
            client.post(url,data, new AsyncHttpResponseHandler() {

                public void onStart() {
                    super.onStart();                                                
                }

                public void onSuccess(String response) {
                }

                public void onFailure(Throwable e, String response) {
                }
            });


        } catch (Throwable e) {
            Log.println(Log.ERROR, GeofencePlugin.TAG, "Exception posting 
    geofence: " + e);    
        }

        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {

     }
   }
   }

Thanks

If you want updated the native code of plugin you should duplicate the git repository and use you'r own cloned repository on you'r config.xml

Like this :

<plugin name="name-of-plugin" spec="https://github.com/**yourrepositoryurl.git**" />

Then, you have to implement a "ajax" call into the onReceive function.

You can find a exemple here : Call PHP function from android?

After you'r code edit :

I thinks the problem was :

com.cowbell.cordova.geofence.TransitionReceiver$PostLocationTask$ is not abstract and does not override abstract method

You can see the doInBackground method was abstract ( Doc here ) and you want to override it from a non abstract class.

I'm not very comfortable with native development but i'm thinks you'r class PostLocationTask must be abstract for override abstract method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM