我正在使用处理程序反复提示用户输入每个例如5分钟。当设备进入休眠模式并且屏幕被锁定时,当我的应用提示用户输入时,如何唤醒设备?我试过这个,但似乎没有用。我在清单中添加了WAKE_LOCK
权限。
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();
}
}
有什么想法吗?
编辑:使用AlarmManager
广播自定义意图。
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();
答案 0 :(得分:4)
通常wakelock
实际开启屏幕。所以你应该用
ACQUIRE_CAUSES_WAKEUP
作为附加标志。
答案 1 :(得分:3)
查看AlarmManager类 http://developer.android.com/reference/android/app/AlarmManager.html 这就像“cron”
好的,这是一段工作代码 - 首先是活动类:
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
public class MainActivity extends Activity {
private WakeLock wl;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//acquire wake lock
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "TAG");
wl.acquire();
// schedule alarm
Intent i = new Intent();
i.setAction(WakeReciever.WAKE_INTENT);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000,
pIntent);
}
@Override
protected void onPause() {
wl.release();
super.onPause();
}
}
接下来是BroadcastReceiver:
package com.test;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
public class WakeReciever extends BroadcastReceiver {
public static final String WAKE_INTENT = "com.test.WAKE";
/**
* @see android.content.BroadcastReceiver#onReceive(Context,Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
//acquire wake lock
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();
//start activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
wl.release();
}
}
最后不要忘记清单文件:
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.test" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".WakeReciever" android:enabled="true">
<intent-filter>
<action android:name="com.test.WAKE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
答案 2 :(得分:0)
从我看到的情况来看,你在获取后立即释放唤醒锁。这就是为什么它给人的印象是不工作。将其移除并将其放在获取它的方法之外的其他位置。
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();
}
};