问题描述
我试图找出如何唤醒和解锁手机的服务。我一直提到这个的职位,但我想不通为什么它不加工。这是code,我到目前为止有:
I am trying to figure out how to wake and unlock the phone with a service. I have been referring to this post but, I can't figure out why it isn't working. This is the code that I have so far:
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!");
}
}
我已经加入了code在清单中也是如此。任何想法?
I have added the code in the manifest as well. Any ideas?
推荐答案
使用这种code以下的为您服务。
Use this code below in your service.
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();
如果你想解锁屏幕,以及,注册您的服务中的接收器,如果屏幕打开/关闭监视器,如果它被关闭,并要解锁手机,开始一项活动与此code。在的onCreate
:
If you want to unlock the screen as well, register a receiver in your service that monitors if the screen is turned on/off and if it is turned off and you want to unlock the phone, start an activity with this code in onCreate
:
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
this.finish();
return;
我知道,这是一个相当肮脏的,但据我所知,有开锁的锁屏的没有别的办法(这会如果没有密码等设置只工作,所以它必须是正常的滑解锁屏幕)。
I know, this is a rather dirty, but as far as I know, there is no other way of unlocking the lockscreen (and this will only work if there are no passwords etc set, so it must be the normal "slide to unlock" screen).
不要忘了添加 android.permission.WAKE_LOCK
; - )
And don't forget to add android.permission.WAKE_LOCK
;-)
/编辑:我刚才看到你已经在使用的活动。如果你有一个,不需要该服务在所有的,只是把这个code进入活动。
/edit: I just saw you are already using an Activity. If you have one and don't need the service at all, just put this code into the activity.
这篇关于安卓:唤醒和放大器;解锁手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!