private void setNotification() {
// TODO Auto-generated method stub
getData();
Calendar cal = Calendar.getInstance();
long when =0;
intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("eventName", eventName);
intent.putExtra("eventDescription",eventDescription);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
RadioGroup rg=(RadioGroup)findViewById(R.id.Setting_rgRepeatMode);
selectedRadio=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
long repeatTime=0;
if(selectedRadio.getText().toString().equals("None"))
{
cal.set(mYear,mMonth,mDay,mHour,mMinute,0);
intent.putExtra("Flag",true);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
when=cal.getTimeInMillis();
am.set(AlarmManager.RTC_WAKEUP,when, pendingIntent);
return;
}
else if(selectedRadio.getText().toString().equals("Daily"))
{
intent.putExtra("Flag",true);
repeatTime=(AlarmManager.INTERVAL_DAY);
}
else if(selectedRadio.getText().toString().equals("Weekly"))
{
intent.putExtra("Flag",true);
repeatTime=(AlarmManager.INTERVAL_DAY*7);
}
else if(selectedRadio.getText().toString().equals("Monthly"))
{
intent.putExtra("month",mDay);
repeatTime=(AlarmManager.INTERVAL_DAY);
}
cal.set(Calendar.HOUR_OF_DAY,mHour);
cal.set(Calendar.MINUTE,mMinute);
cal.set(Calendar.SECOND,0);
when=cal.getTimeInMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC, when,repeatTime, pendingIntent);
}
別のクラスはAlermReceiver.javaです
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
// Bundle bundle = intent.getExtras();
// String message = bundle.getString("alarm_message");
// Toast.makeText(context,bundle.getString("eventName"), Toast.LENGTH_SHORT).show();
NotificationManager notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
int icon = R.drawable.event;
CharSequence notiText = "Event Notification";
long time = System.currentTimeMillis();
@SuppressWarnings("deprecation")
Notification notification = new Notification(icon, notiText,time);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(context, Setting.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context,intent.getStringExtra("eventName"),intent.getStringExtra("eventDescription"), contentIntent);
int SERVER_DATA_RECEIVED = 1;
Calendar cal=Calendar.getInstance();
Toast.makeText(context,"aavechhe "+intent.getBooleanExtra("Flag",false),Toast.LENGTH_SHORT).show();
if(intent.getIntExtra("month",0)==cal.get(Calendar.DAY_OF_MONTH))
{
Toast.makeText(context,"aavechhe "+cal.get(Calendar.DAY_OF_MONTH),Toast.LENGTH_SHORT).show();
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
}
if(intent.getBooleanExtra("Flag",false))
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}