Android event: Ring, vibrate and show alert box
I am trying to create an event in An开发者_开发技巧droid 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
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
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...
精彩评论