Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save deanpanayotov/8861248 to your computer and use it in GitHub Desktop.
Save deanpanayotov/8861248 to your computer and use it in GitHub Desktop.
A simple example. Receiver is declared as inner class of MainActivity. "receiver" block should be added within the "application" block.
<receiver android:name="com.example.test.MainActivity$AlarmReceiver" >
</receiver>
public final void setAlarm(int seconds) {
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
intent, 0);
((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC,
System.currentTimeMillis() + seconds * 1000, pendingIntent);
Toast.makeText(MainActivity.this,
"Timer set to " + seconds + " seconds.", Toast.LENGTH_SHORT)
.show();
}
public static class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d("-", "Receiver4");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment