How to create plugin in phonegap to run the application in background?

3,192 views
Skip to first unread message

Nathi gugan

unread,
Feb 23, 2012, 7:43:41 AM2/23/12
to phonegap
I have created plugin for background service (to run the application
in background) in phonegap.

here is my java code for plugin:

public class BackgroundService extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args,
String callbackId)
{
PluginResult.Status status = PluginResult.Status.OK;
String result = "";

try {
if (action.equals("onCreate")) {
this.onCreate();
}
else if (action.equals("onBind")) {
Intent intent = null;
this.onBind(intent);
}
else if (action.equals("onDestroy")) {
this.onDestroy();
}
else if (action.equals("onStart")) {
Intent intent = null;
this.onStart(intent,args.getInt(1));
}
else if (action.equals("onUnbind")) {
Intent intent = null;
this.onUnbind(intent);
}
return new PluginResult(status, result);
} catch(JSONException e) {
return new
PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
public void onCreate() {

// TODO Auto-generated method stub



}
public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub



return null;

}
public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();



}
public void onStart(Intent intent, int startId) {

// TODO Auto-generated method stub

super.onStart(intent, startId);



}
public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub



}
public void onPause()
{

super.webView.loadUrl("javascript:navigator.BackgroundService.onBackground();");
}

public void onResume()
{

super.webView.loadUrl("javascript:navigator.BackgroundService.onForeground();");
}

}

and my js file is:

function BackgroundService() {
};

BackgroundService.prototype.create = function() {
PhoneGap.exec(null, null, "BackgroundService", "onCreate",
[]);
};

BackgroundService.prototype.destroy = function() {
PhoneGap.exec(null, null, "BackgroundService", "onDestroy",
[]);
};

BackgroundService.prototype.start = function(intent,startId) {
PhoneGap.exec(null, null, "BackgroundService", "onStart",
[intent,startId]);
};

BackgroundService.prototype.bind = function(intent) {
PhoneGap.exec(null, null, "BackgroundService", "onBind",
[intent]);
};

BackgroundService.prototype.unbind = function(intent) {
PhoneGap.exec(null, null, "BackgroundService", "onUnbind",
[intent]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("BackgroundService", new
BackgroundService());
});

and in my index.html. I have added the below code in my button click

navigator.BackgroundService.onCreate();
navigator.BackgroundService.onStart(intent,1);
But I am getting the error.please tell me where I am wrong,please
guide me,thanks in advance.

Aater Suleman

unread,
Feb 23, 2012, 7:51:45 AM2/23/12
to phonegap
Did you change the manifest file correctly?

http://developer.android.com/reference/android/app/Service.html
> super.webView.loadUrl("javascript:navigator.BackgroundService.onBackground( );");
>         }
>
>         public void onResume()
>         {
>
> super.webView.loadUrl("javascript:navigator.BackgroundService.onForeground( );");

Nathi gugan

unread,
Feb 23, 2012, 11:42:39 PM2/23/12
to phonegap
I use this in manifest file
<service android:name=".BackgroundService"
android:process=":remote" />

I got the error in logcat file:
Result of expression 'navigator.BackgroundService' [undefined] is not
an object at file:///android_asset/www/index.html

Please tell me the solution.thanks in advance

ABHISHEK BEDRE

unread,
Jul 24, 2014, 2:17:46 AM7/24/14
to phon...@googlegroups.com, nathi...@gmail.com
To get more details about the phonegap in Android or iOS then please visit...
 


Thank You
ABhishek Bendre

jcesarmobile

unread,
Jul 24, 2014, 2:40:57 AM7/24/14
to phon...@googlegroups.com, nathi...@gmail.com
Please, don't post on old threads just to spam
Reply all
Reply to author
Forward
0 new messages