Background
My application populates a list view containing list of specific times. When the user selects a specific item in the list view, the alarm is scheduled/triggered for that timing. Now I can achieve to create a notification when the alarm starts ringing. But now, I wanted to create an alert dialog box instead of notification. Also, upon the user clicking the OK button on the alert dialog box, the alarm should stop and the alert box should be closed. How can I achieve that?
Also, please explain to me which class to use to call the alert dialog box and which class should I use it to extend and where should I place my intents or pending intents to call the alert dialog box.
P.S: I have used broadcast receiver for my alarm to get scheduled at the selected time from the list of timing.
The class to schedule an alarm
public class mrvtoparanur extends Activity {
int hours,mins;
long time;
CSVAdapter mAdapter;
final static int RQS_1=1;
Calendar cal = Calendar.getInstance();
Calendar calset = (Calendar)cal.clone();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mrvtoparanur);
final ListView mList = (ListView)findViewById(R.id.mrvtoparanurlist);
mAdapter=new CSVAdapter(this,-1);
mList.setAdapter(mAdapter);
mList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
clock clicked=mAdapter.getItem(arg2);
String [] res = clicked.getTime().split(":");
hours=Integer.parseInt(res[0]);
mins=Integer.parseInt(res[1]);
Toast.makeText(getApplicationContext(), "You selected time :"+hours+"hours and "+mins+"mins", Toast.LENGTH_SHORT).show();
ScheduleAlarm();
}
});
}
protected void ScheduleAlarm() {
// TODO Auto-generated method stub
calset.set(Calendar.HOUR_OF_DAY, hours);
calset.set(Calendar.MINUTE, mins);
calset.set(Calendar.SECOND, 0);
Long time = calset.getTimeInMillis();
Intent intentAlarm = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
Toast.makeText(this, "Reminder Set", Toast.LENGTH_SHORT).show();
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Alarm receiver
public class AlarmReciever extends BroadcastReceiver
{
Context context ;
@Override
public void onReceive( Context context, Intent intent)
{
// TODO Auto-generated method stub
// here you can start an activity or service depending on your need
// for example you can start an activity to vibrate phone or to ring the phone
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
Toast.makeText(context, "Alarm Trigerred", Toast.LENGTH_SHORT).show();
}
}
The Clock item
public class clock {
private String t;
public String getTime() {
return t;
}
public void setTime(String t) {
this.t = t;
}
}
My question is: If I have to start an alert as soon as the alarm starts ringing, should I create a separate new class file for alert dialog to display? Or can I embed the code for alert dialog in any of the class above? If i can embed it, then which class should I choose to embed the alert dialog code and from which class should I call the alert dialog?