Skip to content

Instantly share code, notes, and snippets.

@rayshih
Created April 30, 2013 07:44
Show Gist options
  • Save rayshih/5487216 to your computer and use it in GitHub Desktop.
Save rayshih/5487216 to your computer and use it in GitHub Desktop.
// In Manifest:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity> ... </activity>
<receiver android:name=".AlarmReceiver"></receiver>
</application>
// AlarmReceiver
package com.example.alarmmanagertry;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AlarmReceiver", "recieve = =");
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
// To Trigger the alarm
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setData(Uri.parse("content://com.example.alarmmanagertry/" + n));
intent.putExtra("alarm_message", "O'Doyle Rules! n = " + n);
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3 * 1000, sender);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment