[JAVA] I made a lock pattern using the volume key with the Android app. Fragment edition

I wrote a code that unlocks when I type a command within 5 seconds using the volume button.

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {

    //Initialization of commands when changing volume
    mCommandTime = 0;
    mSuccessCommand = "";

    final View v = inflater.inflate(R.layout.fragment_recording, container, false);

    v.setOnKeyListener(new View.OnKeyListener() {
      @Override
      public boolean onKey(View v, int keyCode, KeyEvent event) {


        if (mCommandTime==0) {
          //Once pressed, timer will not start until mCommandTime is reset to 0 after 5 seconds
          mCommandTime++;

          final Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
            @Override
            public void run() {
              Log.d(TAG, "Initialize openPass after 5 seconds");
              mSuccessCommand = "";
              //String the value only when the timer is on(mSuccessCommand)Can be added to
              Log.d(TAG, "The timer can be started after 5 seconds have passed.");
              mCommandTime= 0;
            }
          }, 5000);

        }else{
          Log.d(TAG,"comandTime"+mCommandTime+"And I can't use timer because it's been over 5 seconds");
        }

        if(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP){
          mSuccessCommand=mSuccessCommand+"1";
          Log.d(TAG,"To Command+1, the current command is"+mSuccessCommand);


        }else if(event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN){
          mSuccessCommand=mSuccessCommand+"0";
          Log.d(TAG,"To Command+0, the current command is"+mSuccessCommand);

        }

        if (mSuccessCommand.equals("01101")) { 
          Log.i(TAG,"The life command has been lifted");
     
          Toast.makeText(getActivity(), "The lock has been released.", Toast.LENGTH_SHORT).show();
          
          //Here is the process for unlocking
      }
    });
    // View#It is important to set true in setFocusableInTouchMode
    v.setFocusableInTouchMode(true);
    return v;
  }
}

Although it is not written here, KeyEvent is called twice, "Down" and "Up", so it is good to make an if statement that can be entered only when "Down".

personally,

While making SNS etc. in the future, I would like to adjust the transparency of the application by unlocking the volume button. Have fun

Recommended Posts

I made a lock pattern using the volume key with the Android app. Fragment edition
I made a rock-paper-scissors app with android
I made a matching app (Android app)
I tried using the CameraX library with Android Java Fragment
[Android] I made a pedometer app.
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a chat app.
The basics of the process of making a call with an Android app
I made a command line interface with WinMerge Plugin using JD-Core
[Rails] I made a simple calendar mini app with customized specifications.
I made a shopify app @java
I recently made a js app in the rumored Dart language
I made an app to scribble with PencilKit on a PDF file
I made a risky die with Ruby
I made a bulletin board using Docker 1
Delegate pattern between views. I also made a sample page transition using NavigationLink.
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
I made a gender selection column with enum
Publish the app made with ruby on rails
I made blackjack with Ruby (I tried using minitest)
I made an Android app for MiRm service
[Rails] I made a draft function using enum
I made a LINE bot with Rails + heroku
I made a portfolio with Ruby On Rails
I made a function to register images with API in Spring Framework. Part 1 (API edition)
I tried using a database connection in Android development
A simple CRUD app made with Nuxt / Laravel (Docker)
I made a check tool for the release module
Refactor the Decorator pattern implementation with a functional interface
Vibrate the wristband device with Bluetooth from the Android app
[Ruby] I made a crawler with anemone and nokogiri.
I want to dark mode with the SWT app
I made a library for displaying tutorials on Android.
I made an Android application that GETs with HTTP
I made a function to register images with API in Spring Framework. Part 2 (Client Edition)
I made a program in Java that solves the traveling salesman problem with a genetic algorithm