Skip to content

Instantly share code, notes, and snippets.

@Mehuge
Created September 2, 2016 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mehuge/dad1d66b2d70ccb5b91e7deb5855f35a to your computer and use it in GitHub Desktop.
Save Mehuge/dad1d66b2d70ccb5b91e7deb5855f35a to your computer and use it in GitHub Desktop.
// Based on https://github.com/schchr/cordova-plugin-timers/
private int timerCount = 0;
private Hashtable<Integer, PendingIntent> timerIntents;
AlarmManager alarmManager = null;
private PluginResult setTimeout(JSONArray args) throws JSONException {
final int timerId = ++timerCount;
int time = args.getInt(0);
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final int finalTimerId = timerId;
Activity activity = cordova.getActivity();
activity.runOnUiThread(new Runnable() {
public void run() {
String js = "window.plugins.utility.triggerTimer(" + finalTimerId + ")";
webView.loadUrl("javascript:" + js);
}
});
}
};
Activity activity = cordova.getActivity();
if (null == alarmManager) {
alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
timerIntents = new Hashtable<Integer, PendingIntent>();
}
String name = "RMCv2_TIMER_" + timerId;
activity.registerReceiver(mReceiver, new IntentFilter(name));
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, new Intent(name), 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, pendingIntent);
timerIntents.put(timerId, pendingIntent);
return new PluginResult(PluginResult.Status.OK, timerId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment