2

我正在尝试从我的 phonegap 插件类调用服务。我尝试了此处提供的所有答案,但不能。服务没有被调用。需要帮忙 ..

插件 Java 代码 -

package com.salesforce.androidsdk.phonegap;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * This class echoes a string called from JavaScript.
 */
public class Echo extends CordovaPlugin {
    private static final String TAG = "CordovaPlugin";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Log.i(TAG,"inside execute method--->>>" + action + args + callbackContext);
        if (action.trim().equalsIgnoreCase("echo")) {
            Log.i(TAG,"args.getString(0)--->>>" + args.getString(0));
            String message = args.getString(0); 

            // Initialise the service variables and start it it up
            Context thiscontext = this.cordova.getActivity().getApplicationContext();
            Intent callBackgroundService = new Intent(thiscontext, CallBackgroundService.class);
            callBackgroundService.putExtra("loadinterval", 800); // Set LED flash interval
            thiscontext.startService(callBackgroundService);

            this.echo(message, callbackContext);

            return true;
        }
        return false;
    }

    private void echo(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}

背景服务代码 -

package com.salesforce.androidsdk.phonegap;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class CallBackgroundService extends Service {
    private static final String TAG = "CallBackgroundService";
        @Override
        public void onCreate() {
            super.onCreate();
            Toast.makeText(this, "in create", Toast.LENGTH_LONG).show();
             Log.i(TAG , "inside call background service oncreatye");
        }

      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful
          Log.i(TAG , "inside call background service");
          Toast.makeText(this, "in start", Toast.LENGTH_LONG).show();
          return Service.START_STICKY;
      }

      @Override
      public IBinder onBind(Intent intent) {
      //TODO for communication return IBinder implementation
          Log.i(TAG , "inside call background service");
          Toast.makeText(this, "in start", Toast.LENGTH_LONG).show();
        return null;
      }
} 

在清单中添加了以下行

<service android:exported="true" android:label="@string/service_name" android:enabled="true" android:name="com.salesforce.androidsdk.phonegap.CallBackgroundService" />

但我仍然没有收到任何日志消息或错误。我在这里做错了什么。?使用此代码连续 6 小时无法弄清楚..提前非常感谢

4

0 回答 0