Created
May 31, 2017 19:47
-
-
Save DZuz14/81cf21c45ecd32bb3984dcffa6651da5 to your computer and use it in GitHub Desktop.
Java Module that communicates JS with Native Android AlarmManager
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
// 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.