Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 228595

Cordova plugin not calling a java service [ onStartCommand() ] from plugin class

$
0
0

I am a beginner in cordova . I want to create a plugin for background service to my ionic project. I go through the tutorials of apache cordova , from that I just created a folder structure like


plugin_name

plugin.xml

www

javascript_name.js

src

android

name.java

First i created a simple plugin with hello world alert in application .Next i clubbed this with adding a background service called myService.java makes error , not calling the service . But i successfully called it in android studio as native java ( Only background service ).

I just created a Hello.java and myService.java files in the android folder.

Hello.java

package com.example.plugin;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.widget.Toast;

public class Hello extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {

    if (action.equals("greet")) {

        String name = data.getString(0);
        String message = "Hello, " + name;
		startService();
        callbackContext.success(message);

        return true;

    } else {

        return false;

    }
}


public void startService() {
	Toast.makeText(this, "startService...", Toast.LENGTH_LONG).show();
    Activity context = cordova.getActivity().getApplicationContext();

    Intent intent = new Intent(
            context, MyService.class);

    context.startService(intent);
 }
}

myService.java

package com.example.plugin;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;



/**
 * Created by DewByte on 11-01-2016.
 */
public class MyService extends Service {




@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "Service started...", Toast.LENGTH_LONG).show();


    return START_STICKY;
}

@Override
public void onDestroy() {
    Toast.makeText(this, "Stop service...", Toast.LENGTH_LONG).show();

 }

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


}

and in plugin.xml

<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
    id="com.example.hello"
    version="0.7.0">

<name>Hello</name>

<engines>
<engine name="cordova" version=">=3.4.0"/>
</engines>
<asset src="www/hello.js" target="js/hello.js"/>

<js-module src="www/hello.js" name="hello">
<clobbers target="hello" />
</js-module>

<platform name="android">

<config-file target="res/xml/config.xml" parent="/*">
  <feature name="Hello">
    <param name="android-package" value="com.example.plugin.Hello"/>
  </feature>
</config-file>
  <source-file src="src/android/Hello.java" target-dir="src/com/example/plugin/"/>
  <source-file src="src/android/MyService.java" target-dir="src/com/example/plugin/"/>

</platform>

</plugin>

And in the WWW folder created a hello.js

/*global cordova, module*/

module.exports = {
   greet: function (name, successCallback, errorCallback) {
    cordova.exec(successCallback, errorCallback, "Hello", "greet", [name]);
   }
};

I add the plugin to my ionic project and called the plugin from my app.js like

.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar         above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins &&        window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  cordova.plugins.Keyboard.disableScroll(true);

}
if (window.StatusBar) {
  // org.apache.cordova.statusbar required
  StatusBar.styleDefault();
}



var success = function(message) {
    alert("ok "+message);
}

var failure = function() {
    alert("Error calling Hello Plugin");
}

hello.greet("World", success, failure);


 });
})

Viewing all articles
Browse latest Browse all 228595

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>