Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法触发WakefulBroadcastReceiver_Android_Alarmmanager_Android Pendingintent - Fatal编程技术网

Android 无法触发WakefulBroadcastReceiver

Android 无法触发WakefulBroadcastReceiver,android,alarmmanager,android-pendingintent,Android,Alarmmanager,Android Pendingintent,所以我在做这个项目,在一个确定的时间,手机会自动进入静音模式(没有声音),这里有人建议我使用WakefulBroadcastReceiver。我试图在这里寻找一些关于它如何工作的老话题,并试图在我的代码中实现它,但无论我做什么,它都不会在确定的时间开始。有人能帮我吗?以下是代码,其中hourStart和minuteStart由用户先前确定: public void alarm(){ Calendar cur_cal = new GregorianCalendar(); cur_

所以我在做这个项目,在一个确定的时间,手机会自动进入静音模式(没有声音),这里有人建议我使用WakefulBroadcastReceiver。我试图在这里寻找一些关于它如何工作的老话题,并试图在我的代码中实现它,但无论我做什么,它都不会在确定的时间开始。有人能帮我吗?以下是代码,其中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:我已经在清单上声明了“声音”操作,并启用了用户修改声音的权限。

您应该在清单中指定
唤醒锁定
权限,以便使用
唤醒接收器

谢谢!我不知道。我对Android编程有点陌生。但是,即使使用权限=/,它仍然无法工作。如果我将WakefulBroadcast更改为仅一个广播,并在alarm()函数上发送广播,则效果很好,但这样我无法预先确定发送广播的时间。使用PendingEvent.getService但提供用于接收器的意图似乎有点奇怪。当警报触发时,系统将根据您的接收器意图调用startService。我想你是想挂起帐篷,去广播?是的,你说得对。我以前使用过getBroadcast,但由于它不起作用,我尝试将其更改为getService,只是想看看它是否能以这种方式工作,但后来我修改了权限,将其改回getBroadcast,但仍然不起作用。我还尝试将hourStart和minuteStart更改为固定值,以防我得到的数字出现问题,但正如我所怀疑的那样,情况并非如此。