我正在试图找出如何使用服务唤醒和解锁手机。我一直指的是this帖子,但我无法弄清楚它为什么不起作用。这是我到目前为止的代码:
public class WakephoneActivity extends Activity {
BroadcastReceiver mReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Log.v(TAG, "Screen OFF onReceive()");
screenOFFHandler.sendEmptyMessageDelayed(0, 2000);
}
};
}
private Handler screenOFFHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// do something
// wake up phone
// Log.i(TAG, "ake up the phone and disable keyguard");
PowerManager powerManager = (PowerManager) WakephoneActivity.this
.getSystemService(Context.POWER_SERVICE);
long l = SystemClock.uptimeMillis();
powerManager.userActivity(l, false);// false will bring the screen
// back as bright as it was, true - will dim it
}
};
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);
// Log.i(TAG, "broadcast receiver registered!");
}
}
我也在清单中添加了代码。有什么想法吗?
答案 0 :(得分:14)
在您的服务中使用以下代码。
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "YourServie");
mWakeLock.acquire();
[...]
mWakeLock.release();
如果您想解锁屏幕,请在您的服务中注册接收器,监控屏幕是否开启/关闭以及是否已关闭并且您想要解锁手机,请使用此代码启动活动onCreate
:
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
this.finish();
return;
我知道,这是一个相当脏的,但据我所知,没有其他方法可以解锁锁屏(这只有在没有设置密码等情况下才有效,所以一定是正常的“幻灯片”解锁“屏幕”。
不要忘记添加android.permission.WAKE_LOCK
; - )
/ edit:我刚看到你已经在使用Activity了。如果您有一个并且根本不需要该服务,只需将此代码放入活动中即可。
答案 1 :(得分:1)
要使服务始终处于活动状态,您需要在清单上拥有此权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
您需要做的另一件事是获取WakeLock。没有它,服务将在一段时间后结束。你可以这样做:
getApplicationContext();
PowerManager pm = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
您可能需要将PowerManager.PARTIAL_WAKE_LOCK更改为您需要的那个。您可以看到有关here的信息。
答案 2 :(得分:1)
有WakefulBroadcastReceiver
为您执行此操作。使用示例:
import android.content.Context; import android.content.Intent; import android.os.SystemClock; import android.support.v4.content.WakefulBroadcastReceiver; import android.util.Log; public class SimpleWakefulReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // This is the Intent to deliver to our service. Intent service = new Intent(context, SimpleWakefulService.class); // Start the service, keeping the device awake while it is launching. Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime()); startWakefulService(context, service); } }
完成服务中的操作后,请致电SimpleWakefulReceiver.completeWakefulIntent(intent)
以解除唤醒锁定。
(因为@Force已经提供了关于wakeLock的详细信息,不需要在这里重复; - )
请注意,该类已从api级别26.1.0,参考here
弃用