我用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);
}
}
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);
}
}
解决方案 »
- 新浪微博 获取话题接口
- 点击不同的Button跳转到同一个activity但显示的界面不同。
- 看到很多Android程序里面传Context对象,但不知道这个对象怎么理解,对应AP的实例还是什么其它?
- android SQLite 查询数据时总查不到,以前是可以的,求解啊!
- 联系人自定义字段更新问题
- 怎么判断用户拉下了系统通知栏?
- 将view添加到WindowManager时如何获得View所需大小的问题?
- apk安装时版本问题
- 问一个关于MediaPlayer的问题
- 安卓上传多张照片内存溢出
- Android中一个应用有几个window
- 安卓问题 照着第一行代码敲的 第14章天气app实战 在点击listview的item时 一直等待出不来结果
AlarmManager.RTC_WAKEUP 应该就是不管休眠还是关机都唤醒,我不太明白为什么。
AlarmManager.RTC_WAKEUP 应该就是不管休眠还是关机都唤醒,我不太明白为什么。这个并不是一定的,要看系统的,比如我用过的全志的一个版本就是
AlarmManager.RTC_WAKEUP 应该就是不管休眠还是关机都唤醒,我不太明白为什么。这个并不是一定的,要看系统的,比如我用过的全志的一个版本就是
有什么解决的方案或者思路吗?