开发者

How to set phone's alarm from program code in Android

I want to set phones alarm in phone. Can anybody say how to do that?

I got t开发者_如何学Che code from net, and try but the alram not set to phone.

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Intent activate = new Intent(this, Alaram.class);
AlarmManager alarams ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarams = (AlarmManager) getSystemService(ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);

In this code we are provide intent as Alaram.class. Then what should be written in this Alram.class so that alarm will set to the mobile.


I think you misunderstood what the AlarmManager is good for. It starts your code on a given Point, it doesn't set the phones ring tone.

If you want to set the Phones Alarm sound, this might be helpful (from the standard Android Music Player):

// Set the system setting to make this the current ringtone
MusicUtils.setRingtone(this, mService.getAudioId());


You are setting alarm time as current time in millisecond it will be exceed when the alarmmanager wake up....So,here i modified your code.check this it will work.That alaram will start after 5 seconds...i mean your Alaram activity will start within 5 seconds.

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Intent activate = new Intent(this, Alaram.class);
AlarmManager alarams ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarams = (AlarmManager) getSystemService(ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+5000, alarmIntent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜