-1

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?

Andrew T.
  • 4,592
  • 7
  • 38
  • 55
  • You can try [this official guide](http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog) first if you're challenged to do it by yourself. Hint: you can put the code inside the alarm receiver. (Not tested, but I think it's possible). Or instead, follows Guillaume's advice: create activity with dialog theme instead. – Andrew T. May 02 '14 at 03:33
  • Actually I had already got the tutorial that you had given in this link. Anyways appreciate that ! Btw Andrew, in the same link, they had given the first statement in the code `AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());` I need to know, in the place of ***getActivity()*** which class should i use??? – user3580022 May 02 '14 at 10:54

2 Answers2

0

You can create a new activity with a dialog layout when you receive your event. But I am not sure it is a good idea to display a dialog box. The user will be annoying. Why not keeping the notification ?

Guillaume
  • 144
  • 2
  • 11
  • I feel this should be posted as comment instead since it doesn't really solve the problem. You just need 1 more rep to have that privilege! Suggest quality edit somewhere and come back here. Else, expand this answer: how to open activity instead when alarm rings? how to display it as dialog? – Andrew T. May 02 '14 at 03:43
  • Yes I think that too but I couldn't do that. I completly agree with you but unfortunatelyI have not enought reputation to post a comment – Guillaume May 02 '14 at 04:10
  • But i would wanna give a try for both of them.. I had already created a notification code which is working good.. but I wanna give a try to dialog box too... – user3580022 May 02 '14 at 11:00
0

Download the android source code from https://source.android.com/ and look at the ./packages/apps/DeskClock/src/com/android/deskclock/AlarmAlertFullScreen.java code. It appears to do just what you are describing.

dangVarmit
  • 5,403
  • 2
  • 19
  • 23
  • Sorry dang.. but this is a general link... where am i to search for the desk clock source code?sorry but im completely a newbie when it comes to android. – user3580022 May 02 '14 at 10:59
  • The link is where/how you can download the full android source. That's where you can see how the stock alarm clock does it – dangVarmit May 02 '14 at 17:02