I는 장치가 잠겨 있는지 확인하는 나사를 사용함으로써 해결했다.
서비스의 코드 부분은 :
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("Receiver", "Screen ON");
} else {
new Thread(new mRunnable()).start();
}
}
};
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClass(getBaseContext(), activity.class);
startActivity(i);
break;
}
super.handleMessage(msg);
}
};
class mRunnable implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
if (isLocked()) {
mHandler.sendEmptyMessage(1);
Thread.currentThread().interrupt();
}
else {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
}
private boolean isLocked() {
mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
return mKeyguardManager.inKeyguardRestrictedInputMode();
}
나는 답을 찾는 다른 사람을 위해 여기에 나열된. 솔루션으로 초안을 작성 했으므로 아직 코드와 프로그램의 성능을 고려하지 않았습니다.
감사합니다. AVD 또는 i9300에서 테스트했습니다. def 값이 0이므로 AVD도 0을 반환합니다. my i9300이 올바른 값을 얻는 것처럼 보입니다. 그렇다면이 코드가 모든 실제 장치에서 올바른 값을 얻을 수 있는지 궁금합니다. – Chiman
나는 안드로이드 4.2.2에서 테스트를했으며 잘 작동한다. 일부 최신 버전에서는 설정이 보안으로 이동되었으므로 이전 버전에서는 작동하지 않습니다. 안드로이드 버전이 작동하지 않는 것을 말할 수 있습니까? – dinesh
Android 4.3 이상에서는 작동하지 않습니다. – user2891462