http://ccdev.tistory.com/16 소스 참고했습니다.
public class ScreenReceiver extends BroadcastReceiver {
private KeyguardManager km = null;
private KeyguardManager.KeyguardLock keyLock = null;
private TelephonyManager telephonyManager = null;
private boolean isPhoneIdle = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
if(km==null)
km=(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if(keyLock==null)
keyLock = km.newKeyguardLock(Context.KEYGUARD_SERVICE);
if (telephonyManager == null){
telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
if(isPhoneIdle) {
disableKeyguard();
Intent i = new Intent(context, LockScreenActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
public void reenableKeyguard() {
keyLock.reenableKeyguard();
}
public void disableKeyguard() {
keyLock.disableKeyguard();
}
private PhoneStateListener phoneListener = new PhoneStateListener(){
@Override
public void onCallStateChaged(int state, String incomingNumber){
switch (state){
case TelephonyManager.CALL_STATE_IDLE:
isPhoneIdle = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
isPhoneIdle = false;
break;
}
}
};
}
오류
Error:(56, 9) error: method does not override or implement a method from a supertype
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.