我用AlarmManager启动一个闹钟程序,到了时间发送一个广播,在广播接收器里面弹出一个activity,在activity中播放一段音效。现在有一个问题是。手机黑屏一段时间后,提示音效就不再播放,只有解开锁点亮屏幕之后,才出提示。应该是因为休眠了吧。有什么解决方案吗?我把相关代码贴一下。闹钟设置的代码:Calendar c=Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());   
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); 
        PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() + SPACE, INTERVAL, pi);闹钟模式设置为AlarmManager.RTC_WAKEUP应该没有什么问题吧。这里是广播接收器代码:public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(context, "时间到了", Toast.LENGTH_SHORT).show();
Log.i("barcode", "receive");
Intent i=new Intent(context, AlarmActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
}
}
这里是接受以后启动的弹窗代码
public class AlarmActivity extends Activity {
@ViewInject(R.id.cancle_btn)
Button canBtn;
@ViewInject(R.id.sure_btn)
Button sureBtn;
private SoundPool soundPool;
Map<Integer, Integer> soundMap = new HashMap<Integer, Integer>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alarm);
ViewUtils.inject(this);
Log.i("barcode", "onCreate");
} private void initSound() {
soundPool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 1);
soundMap.put(1, soundPool.load(this, R.raw.remind, 1));
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1);
}
});
//2分钟后释放soundpool
releaseSound();
} @OnClick(R.id.cancle_btn)
public void cancleEven(View v) {
this.finish();
} @OnClick(R.id.sure_btn)
public void sureEven(View v) {
// 注销疲劳驾驶提醒
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class); // 创建Intent对象
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
am.cancel(pi);
this.finish();
} @Override
protected void onResume() {
super.onResume();
initSound();
Log.i("barcode", "onResume");
} @Override
protected void onDestroy() {
super.onDestroy();
if (null != soundPool) {
soundPool.release();
}
Log.i("barcode", "onDestroy");
}

/**
 * 提示2分钟以后,释放soundPool以节省系统资源
 */
private void releaseSound(){
new Handler().postDelayed(new Runnable(){   
    public void run() {
     if (null != soundPool) {
soundPool.release();
}
    }   
 }, 1000 * 60 * 2);   
}
}

解决方案 »

  1.   

    这个alarm只能暂时唤醒cpu,你那个延迟执行不好吧。
      

  2.   

    要看系统支不支持闹钟唤醒,如果支持,把闹钟类型换成相应的值就可以了,就是更换你代码中的AlarmManager.RTC_WAKEUP值,如果有系统源码,可以查看唤醒闹钟相应值的定义
      

  3.   


    AlarmManager.RTC_WAKEUP 应该就是不管休眠还是关机都唤醒,我不太明白为什么。
      

  4.   

    延迟执行不好什么意思?延迟只是用来回收soundpool的啊?现在连响都没响还没到回收的地步啊。
      

  5.   


    AlarmManager.RTC_WAKEUP 应该就是不管休眠还是关机都唤醒,我不太明白为什么。这个并不是一定的,要看系统的,比如我用过的全志的一个版本就是
      

  6.   


    AlarmManager.RTC_WAKEUP 应该就是不管休眠还是关机都唤醒,我不太明白为什么。这个并不是一定的,要看系统的,比如我用过的全志的一个版本就是
    有什么解决的方案或者思路吗?