Pergunta

I am trying to create an event in Android and when the time of the scedules event is reach, I want to display an alert box, ring and vibrate the phone. Can someone please help on this? Thanks

Foi útil?

Solução

From the Handler class documentation

Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

More information can be found here. In your case it seems that you need either postAtTime or postDelayed. The code would look something like

new Handler().postDelayed(new Runnable() {
    public void run() {
        //show alert, vibrate and ring..
    }
}, 10000);//execute this Runnable in 10 sec

Outras dicas

You will need to set an alarm using the AlarmManager. Then you need a BroadcastReceiver to perform your tasks when the time is up. To ring you would need to use the MediaPlayer using the URI of your preferred sound (notification / ringtone / alarm) and to vibrate you will be using the Vibrator service: Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

Now that you have the general idea I suggest you read some examples and the Android website for more details...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top