I'm creating an android reminder application using AlarmManager. My main activity's GUI contains a time and datepicker dialog and a button that calls setReminder function which fetches time and date that user enters and sets reminder for that particular date and time . When the reminder time occurs it moves to another activity whose GUI contains buttons for snooze and cancel. I played the alarm using MediaPlayer. Both buttons are working well. I want my alarm to run in the background even if the application is closed and want my app to notify the OS about the alarm. That is why I want to add broadcast receiver and service to this application. How can I do that?
Main Activity code:
public class MainActivity extends Activity {
AlarmManager alarmMgr;
private Calendar calendar;
private TextView dateView;
private int year, month, day;
private int hour,min;
// private int sYear, sMonth, sDay;
//private int sHour,sMin;
private EditText time;
private String format = "";
PendingIntent pi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateView = (TextView) findViewById(R.id.editText2);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);
time = (EditText) findViewById(R.id.editText3);
calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY);
min = calendar.get(Calendar.MINUTE);
showTime(hour, min);
}
@SuppressWarnings("deprecation")
public void setDate(View view) {
showDialog(999);
Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
.show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
return new DatePickerDialog(this,myDateListener, year, month, day);
}
else if (id == 1111) {
return new TimePickerDialog(this,myTimeListener, hour,min,true);
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
showDate(arg1, arg2+1, arg3);
}
};
private void showDate(int year, int month, int day) {
dateView.setText(new StringBuilder().append(day).append("/")
.append(month).append("/").append(year));
this.year=year;
this.month=(month-1);
this.day=day;
}
@SuppressWarnings("deprecation")
public void setTime(View view) {
showDialog(1111);
Toast.makeText(getApplicationContext(), "set", Toast.LENGTH_SHORT)
.show();
}
private TimePickerDialog.OnTimeSetListener myTimeListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
showTime(arg1,arg2);
}
};
public void showTime(int hour, int min) {
time.setText(new StringBuilder().append(hour).append(" : ").append(min)
.append(" ").append(format));
this.hour= hour;
this.min= min;
}
public void setAlarm(View v)
{
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(),Alarm.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(),0,intent,0);
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
long timeToAlarm = calendar.getTimeInMillis();
alarmMgr.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Main xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/button1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="setAlarm"
android:text="Set" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:ems="10" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignLeft="@+id/textView2"
android:layout_marginBottom="33dp"
android:onClick="setDate"
android:text="Set Date" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="104dp"
android:ems="10" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:layout_alignLeft="@+id/textView2"
android:onClick="setTime"
android:text="SetTime" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="67dp"
android:text="Set Hobby"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Alarm.java
public class Alarm extends Activity {
MediaPlayer mp;
AlarmManager AlmMgr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
mp=MediaPlayer.create(getApplicationContext(), R.raw.tum);
mp.start();
}
public void onCancel(View v)
{
mp.stop();
mp=null;
}
public void onSnooze(View v)
{
mp.stop();
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
long snoozeTime = calendar.getTimeInMillis();
Intent AlarmIntent = new Intent(getApplicationContext(),this.getClass());
AlarmIntent.putExtra("REQUEST CODE", 1);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlmMgr.set(AlarmManager.RTC_WAKEUP, snoozeTime, pi);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_alarm, menu);
return true;
}
}
activity_alarm_xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Alarm" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="67dp"
android:onClick="onCancel"
android:text="Cancel" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="48dp"
android:text="Snooze"
android:onClick="onSnooze" />
</RelativeLayout>
I want to add broadcast receiver and service in this application. How can I do that?
Well, it will be impossible to help unless you tell us what the purpose of both, what you want to do with them, and what you already tried.
Add class of BradcastReceiver:
public class MySnoozeReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent = new Intent(context, Alarm .class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Don't forget the manifest:
<receiver android:name='.MySnoozeReciever'>
</receiver>
Modify those lines:
Intent intent = new Intent(getApplicationContext(), MySnoozeReciever.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),0,intent,0);