不通过PhoneGap插件代码调用服务

时间:2013-05-05 14:32:15

标签: android cordova plugins service phonegap-plugins

我正在尝试从我的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.");
        }
    }
}

BackgroundService代码 -

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个小时直接弄清楚了......很多事先感谢

0 个答案:

没有答案