I am trying to create reminder using alarm manager. But seems it's not working. I am trying to set multiple reminder using different id but my broadcastreceiver not getting called. I am not seeing any notification or nor any sound.
I have tried 30-40 times.
I am setting date to calender like 25/11/2014
Here is myactivity code which setting alarm notification.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DAY_OF_MONTH, 25);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 30);
//calendar.set(Calendar.SECOND, 1);
Intent myIntent = new Intent(CreateReminder.this, MyReceiver.class);
myIntent.putExtra("reminder_id",reminderid);
myIntent
.setAction("com.sandeep.alarm.REMINDER");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
CreateReminder.this, reminderid, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
MyReceiver.class
public class MyReceiver extends BroadcastReceiver {
private Ringtone r;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
int ID=intent.getExtras().getInt("reminder_id");
Log.i("CreateReminder", "reminder_id:-"+ID);
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "reminder", when);
String title = context.getString(R.string.app_name);
String subTitle = "Please complete task";
Intent notificationIntent = new Intent(context, ReminderActivity.class);
notificationIntent.putExtra("reminder_id", ID);
PendingIntent intent1 = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, subTitle, intent1);
//To play the default sound with your notification:
//notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_INSISTENT;
//notification.defaults |= Notification.;
notificationManager.notify(0, notification);
Uri notification1 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(context, notification1);
r.play();
new Handler().postDelayed(new Runnable(){
public void run() {
r.stop();
}
}, 10000);
}
}
I am registering my receiver in AndroidManifest.xml with permissions.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sandip.remindme.CreateReminder"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.sandip.remindme.ReminderActivity"></activity>
<receiver android:name="com.sandip.remindme.MyReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.sandeep.alarm.REMINDER" />
</intent-filter>
</receiver>
</application>
I don't understand why it's not working. Please give me some hints or reference.
You are not sending your broadcast after you set your action in your intent.