Skip to content

Instantly share code, notes, and snippets.

@DZuz14
Created May 31, 2017 19:47
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 DZuz14/81cf21c45ecd32bb3984dcffa6651da5 to your computer and use it in GitHub Desktop.
Save DZuz14/81cf21c45ecd32bb3984dcffa6651da5 to your computer and use it in GitHub Desktop.
Java Module that communicates JS with Native Android AlarmManager
// Android & Java
import android.widget.Toast;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import java.util.Calendar;
// React
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class AlarmManagerModule extends ReactContextBaseJavaModule {
// Constructor
public AlarmManagerModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "AlarmManager";
}
@ReactMethod
public void setAlarm(int hour, int minute) {
Context context = getReactApplicationContext();
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) getReactApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pintent);
Toast.makeText(context, "Alarm set!",Toast.LENGTH_LONG).show();
}
}
@DZuz14
Copy link
Author

DZuz14 commented May 31, 2017

Lot more to add to this. This module will eventually house several different public bridge functions to Javascript that allow for all different types of alarms. Right now this just works off of UTC Wall Clock Time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment