I've found this code some days ago but it doesn't work. Can somebody explain me why? I want to detect if user locked device or not. It has to print Point 2, Point 1 and Point 2(if phone was unlocked then locked and unlocked again) but it prints only Point 2 and that's all. Nothing more. My Activity:
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(new Intent(MainActivity.this, MyService.class));
            }
        });
    }
}
MyService:
public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        KeyguardManager kgMgr =
                (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        boolean showing = kgMgr.inKeyguardRestrictedInputMode();
        if (showing){
            Log.i("Point ", "1");
        }else{
                Log.i("Point ", "2");
            }
        }
    }
}
 
    