所以我正在做这个项目,在确定的时间,手机将自动进入静音模式(没有声音),这里有人建议我使用WakefulBroadcastReciever。我试着在这里查找一些关于它如何工作的旧主题,并尝试在我的代码上实现它,但无论我做什么,它都不会在确定的时间开始。有人能帮我吗?以下是代码,其中hourStart和minuteStart由用户先前确定:
public void alarm(){
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, hourStart);
cal.set(Calendar.MINUTE, minuteStart);
Log.d("calendartimeValue", String.valueOf(cal.getTimeInMillis()));
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(this, SoundWakefulReceiver.class);
intent.setAction("SOUND_ACTION");
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
}
public class SoundWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, SoundService.class);
Log.d("receiver", "receiver here !!");
startWakefulService(context, service);
}
}
public class SoundService extends IntentService {
public SoundService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
AudioManager amanager;
Log.d("service","service started!!");
amanager = (AudioManager)getSystemService(AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
SoundWakefulReceiver.completeWakefulIntent(intent);
}
}
p.s:我已经在清单上声明了“SOUND_ACTION”并启用了用户权限来修改声音。
答案 0 :(得分:0)
您应在清单中指定WAKE_LOCK
权限才能使用WakefulBroadcastReceiver