【问题标题】:How to make call to PHP file in background when Transition happen (ENTER & EXIT ) cordova geofence plugin in background?如何在后台发生转换(ENTER&EXIT)cordova geofence插件时在后台调用PHP文件?
【发布时间】:2019-03-07 13:25:03
【问题描述】:

我只想在 background 中发生转换(ENTER & EXIT)时调用位于服务器中的 php 文件。我读 我需要创建 TransitionReceiver.java 文件来实现此目的的 cordova 地理围栏插件文档。

我读到了这个native implementation

任何帮助

我试过下面的代码:

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) {

     }
   }
   }

谢谢

【问题讨论】:

    标签: java cordova cordova-plugins android-geofence


    【解决方案1】:

    如果你想更新插件的本地代码,你应该复制 git 存储库并在你的 config.xml 上使用你自己的克隆存储库

    像这样:

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

    然后,您必须在 onReceive 函数中实现“ajax”调用。

    你可以在这里找到一个例子:Call PHP function from android?

    代码编辑后:

    我认为问题是:

    com.cowbell.cordova.geofence.TransitionReceiver$PostLocationTask$ 是 非抽象且不覆盖抽象方法

    你可以看到 doInBackground 方法是抽象的 (Doc here) 并且你想要 从非抽象类覆盖它。

    我对原生开发不是很满意,但我认为你的类 PostLocationTask 必须是抽象的才能覆盖抽象方法

    【讨论】:

    • 我会试试这个,让你知道谢谢
    • 嗨恩佐,请检查我的代码意思是,如果你不介意,我做对了吗
    • 我修改了我的帖子来回答你
    • Enzo 请检查我的代码并告诉我哪里做错了
    • 尝试将您的代码放入没有 android 的 java 类中,让我们看看它是否工作,如果不工作并且不返回任何错误,请使用 wireshark 检查您的网络以观察什么请求确实
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    相关资源
    最近更新 更多