唤醒设备时,应用程序会提示用户 [英] Wake the device up when app prompts user
问题描述
我使用的处理程序反复提示用户输入每个如5分钟。当设备进入睡眠模式,屏幕被锁定,我怎么能唤醒设备时,我的应用程序将提示用户输入?我试过,但它似乎并没有工作。我已经在清单中添加 WAKE_LOCK
许可。
类BtHandler扩展处理程序{
私人电源管理器时;
私人WakeLock WL;
@覆盖
公共无效的handleMessage(信息MSG){
PM =(电源管理器)FixedNode.this.getSystemService(Context.POWER_SERVICE);
如果(!pm.isScreenOn()){
WL = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,TAG);
wl.acquire();
}
FixedNode.this.setAlwaysDiscoverable();
wl.release();
}
}
任何想法?
编辑:使用 AlarmManager
播放定制的意图
mReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
振动器V =(震动)getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(300);
电源管理器PM =(电源管理器)getSystemService(Context.POWER_SERVICE);
KeyguardManager公里=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
WakeLock WL = NULL;
如果(!pm.isScreenOn()){
KeyguardLock KL = km.newKeyguardLock(TAG);
kl.disableKeyguard();
WL = pm.newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP,TAG);
wl.acquire();
}
Toast.makeText(上下文,报警工作,Toast.LENGTH_LONG).show();
wl.release();
}
};
MFILTER =新的IntentFilter(ACTION_NAME);
意图mIntent =新的意图(ACTION_NAME);
PendingIntent pendingIntent = PendingIntent.getBroadcast(此,0,mIntent,0);
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+(120 * 1000),pendingIntent);
Toast.makeText(这一点,报警设置,Toast.LENGTH_LONG).show();
通常 wakelock
犯规实际的开启的屏幕上。所以,你应该得到唤醒锁
ACQUIRE_CAUSES_WAKEUP
作为一个额外的标志。
I'm using handler to repeatedly prompt user for an input every e.g. 5 minutes. When the device goes into sleeping mode and screen is locked, how can I wake the device up when my app prompts user for input? I've tried this but it doesn't seem to work. I've added WAKE_LOCK
permission in the manifest.
class BtHandler extends Handler {
private PowerManager pm;
private WakeLock wl;
@Override
public void handleMessage(Message msg) {
pm = (PowerManager)FixedNode.this.getSystemService(Context.POWER_SERVICE);
if (!pm.isScreenOn()) {
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "TAG");
wl.acquire();
}
FixedNode.this.setAlwaysDiscoverable();
wl.release();
}
}
Any ideas?
Edit: Using AlarmManager
to broadcast custom intent.
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(300);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
WakeLock wl = null;
if (!pm.isScreenOn()) {
KeyguardLock kl = km.newKeyguardLock("TAG");
kl.disableKeyguard();
wl = pm.newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();
}
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
wl.release();
}
};
mFilter = new IntentFilter(ACTION_NAME);
Intent mIntent = new Intent(ACTION_NAME);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mIntent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (120 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
Normally wakelock
doesnt actual turn on the screen. So you should get the wake lock with
ACQUIRE_CAUSES_WAKEUP
as an additional flag.
这篇关于唤醒设备时,应用程序会提示用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!