Created
November 3, 2011 15:43
-
-
Save ChrisMcKee/1336815 to your computer and use it in GitHub Desktop.
Loading Dialog Phonegap Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(!window.plugins) { | |
window.plugins = {}; | |
} | |
window.plugins.loadingIndicator = { | |
show: function(options, callback){ | |
window.PhoneGap.exec( | |
callback, | |
null, | |
'LoadingIndicator', | |
'showLoading', | |
[options] | |
); | |
}, | |
hide: function(callback){ | |
window.PhoneGap.exec( | |
callback, | |
null, | |
'LoadingIndicator', | |
'hideLoading', | |
[] | |
); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.phonegap.plugins; /*or just use your own namespace*/ | |
import org.json.JSONArray; | |
import android.app.ProgressDialog; | |
import android.util.Log; | |
import com.phonegap.DroidGap; | |
import com.phonegap.api.Plugin; | |
import com.phonegap.api.PluginResult; | |
import com.phonegap.api.PluginResult.Status; | |
public class PGLoadingDialog extends Plugin { | |
final static String TAG = PGLoadingDialog.class.getSimpleName(); | |
static PGLoadingDialog instance = null; | |
static ProgressDialog dialog = null; | |
public static final String LOADING = "showLoading"; | |
public static final String FINISHEDLOADING = "hideLoading"; | |
public PGLoadingDialog() { | |
instance = this; | |
} | |
public static PGLoadingDialog getInstance() { | |
return instance; | |
} | |
private Thread t; | |
@Override | |
public PluginResult execute(String action, JSONArray data, String callbackId) { | |
PluginResult result = new PluginResult(Status.INVALID_ACTION); | |
final DroidGap droidGap = (DroidGap)this.ctx; | |
if(LOADING.contains(action)){ | |
Log.i(TAG, "------ Spin ON"); | |
t = new Thread(new Runnable() { | |
public void run() { | |
ctx.runOnUiThread(new Runnable() { | |
public void run() { | |
droidGap.spinnerStart("", "Loading. Please wait..."); | |
} | |
}); | |
} | |
}); | |
t.start(); | |
result = new PluginResult(Status.OK); | |
} | |
else if(FINISHEDLOADING.contains(action)){ | |
Log.i(TAG, "------ Spin OFF"); | |
droidGap.spinnerStop(); | |
t.stop(); | |
result = new PluginResult(Status.OK); | |
} | |
return new PluginResult(Status.OK); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Add this into /res/xml/plugins.xml --> | |
<plugin name="LoadingIndicator" value=" com.phonegap.plugins.PGLoadingDialog" /> |
Thanks! I updated this for cordova 2.1.0
//Show it
Indicator.show("",show);
var show = function(error){
};
//Hide it
Indicator.hide("",hide);
var hide = function(error){
};
//JS
var Indicator = {
show: function(options, callback){
window.Cordova.exec(
callback,
null,
'LoadingIndicator',
'showLoading',
[options]
);
},
hide: function(callback){
window.Cordova.exec(
callback,
null,
'LoadingIndicator',
'hideLoading',
[]
);
}
};
//JAVA
package com.foregroundgalleryplugin;
import org.json.JSONArray;
import android.app.ProgressDialog;
import android.util.Log;
import org.apache.cordova.DroidGap;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
public class PGLoadingDialog extends Plugin {
final static String TAG = PGLoadingDialog.class.getSimpleName();
static PGLoadingDialog instance = null;
static ProgressDialog dialog = null;
public static final String LOADING = "showLoading";
public static final String FINISHEDLOADING = "hideLoading";
public PGLoadingDialog() {
instance = this;
}
public static PGLoadingDialog getInstance() {
return instance;
}
private Thread t;
@SuppressWarnings("deprecation")
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
@SuppressWarnings("unused")
PluginResult result = new PluginResult(Status.INVALID_ACTION);
final DroidGap droidGap = (DroidGap)this.cordova.getContext();
if(LOADING.contains(action)){
Log.i(TAG, "------ Spin ON");
t = new Thread(new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
ctx.runOnUiThread(new Runnable() {
public void run() {
droidGap.spinnerStart("", "Loading. Please wait...");
}
});
}
});
t.start();
result = new PluginResult(Status.OK);
}
else if(FINISHEDLOADING.contains(action)){
Log.i(TAG, "------ Spin OFF");
droidGap.spinnerStop();
t.stop();
result = new PluginResult(Status.OK);
}
return new PluginResult(Status.OK);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to write call plugin in javascript ?