我需要在特定时间静音手机。时间由用户提供。 我是android的新手。
答案 0 :(得分:1)
以下是设置闹钟管理器的代码:
private void settingAlarmManager (){
int requestCode = 12345
calendar.set(selectedYear, selectedMonth - 1, selectedDay, selectedHour, selectedMinutes, 0); // For giving Date and Time for alert \\
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(AddTaskActivity.this, AlarmReceiver.class);
PendingIntent broadcast = PendingIntent.getBroadcast(AddTaskActivity.this,
requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast);
}
这是清单代码:
<receiver android:name=".alarm.AlarmReceiver"/>
这是警报接收器类:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//am.setRingerMode(0); // For Silent \\
am.setRingerMode(1); // For Vibrate \\
//am.setRingerMode(2); // For Ringer \\
}
}
答案 1 :(得分:0)
使用PendingIntent在指定时间触发事件,并使用AudioManager更改振铃模式,如下所示:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);