[android] How to unlock android phone through ADB

My screen is dead and I want to unlock my phone so I can access it through Kies to backup my pictures.

I locked the phone through Android Device Manager setting an easy password (was hoping for an Unlock option once I locked it) and tried various methods to unlock it for example:

adb shell input text 1234

Since I don't know what the lock screen look like I'm not sure of the correct inputs to unlock it.

I know the phone is on and that it responds to adb. I am also able to run apps on it through Eclipse. It's a Samsung Galaxy S5 with Android 5.0.

This question is related to android adb

The answer is


Below commands works both when screen is on and off

To lock the screen:

adb shell input keyevent 82 && adb shell input keyevent 26 && adb shell input keyevent 26

To lock the screen and turn it off

adb shell input keyevent 82 && adb shell input keyevent 26

To unlock the screen without pass

adb shell input keyevent 82 && adb shell input keyevent 66

To unlock the screen that has pass 1234

adb shell input keyevent 82 && adb shell input text 1234 && adb shell input keyevent 66

Tested in Nexus 5:

adb shell input keyevent 26 #Pressing the lock button
adb shell input touchscreen swipe 930 880 930 380 #Swipe UP
adb shell input text XXXX #Entering your passcode
adb shell input keyevent 66 #Pressing Enter

Worked for me.


Another way just for your information.

Use an USB OTG cable and connect with an USB mouse, you can touch the screen by clicking your mouse !


This command helps you to unlock phone using ADB

adb shell input keyevent 82 # unlock

If you had MyPhoneExplorer installed and connected (not sure this is a must, happened to be my setup already), you could use it to control the screen with your computer mouse. It connects via ADB, for which your normal USB cable is enough.

Another solution I found that even worked without a reboot is updating tables in settings.db and locksettings.db I had to switch to root to open the settings.db though:

 adb shell
 su
 sqlite3 /data/data/com.android.providers.settings/databases/settings.db
 update secure set value=1 where name='lockscreen.disabled';
 .quit
 sqlite3 /data/system/locksettings.db
 update locksettings set value=0 where name='lock_pattern_autlock';
 update locksettings set value=1 where name='lockscreen.disabled';
 .quit

Source that made me edit my tables


If you want to open your phone without touching it here is the way

Steps

  1. Make sure you have completed the adb setup in both pc and android
  2. open cmd(Command Prompt)
  3. type adb devices to cheek if your phone is ready or not
  4. If it shows something like
List of devices attached
059c97f4        device

then enter the following command

adb shell input keyevent 26 && adb shell input swipe 600 600 0 0 && adb shell input text <pass> && adb shell input keyevent 66

put your password in <pass> and done. You phone is hopefully opened


I would like to share my way, first of all i had Huawei ascend p7 and my touch screen stopped handling touch, so none of the above solutions helped me to be unlock the phone, i have found a better clever way to do it since i can see the screen on thus i thought that my display is 1080 x 1920 px thus i had to simulate a drawing on my photoshop with keypad with (x,y) so i can try instead with input mouse tap command.

screenshot of simulation

Since i have pin lock as you can see in the picture, i have got all the (x,y) for all the numbers on the screen to simulate touch and unlock my screen and have to backup my data, thus if my password is 123 i did all the following commands

adb shell input mouse tap 100 1150
adb shell input mouse tap 500 1150
adb shell input mouse tap 900 1150

And then my phone just got unlocked, i hope it was helpful.


If you have USB-Debugging/ADB enabled on your phone and your PC is authorized for debugging on your phone then you can try one of the follwing tools:

scrcpy

scrcpy connects over adb to your device and executes a temporary app to stream the contents of your screen to your PC and you're able to remote control your device. It works on GNU/Linux, Windows and macOS.

Vysor

Vysor is a chrome web app that connects to your device via adb and installs a companion app to stream your screen content to the PC. You can then remote control your device with your mouse.

MonkeyRemote

MonkeyRemote is a remote control tool written by myself before I found Vysor. It also connects through adb and lets you control your device by mouse but in contrast to Vysor, the streamed screen content updates very slow (~1 frame per second). The upside is that there is no need for a companion app to be installed.


I had found a particular case where swiping (ADB shell input touchscreen swipe ... ) to unlock the home screen doesn't work. More exactly for Acer Z160 and Acer S57. The phones are history but still, they need to be taken into consideration by us developers. Here is the code source that solved my problem. I had made my app to start with the device. and in the "onCreate" function I had changed temporarily the lock type.

Also, just in case google drive does something to the zip file I will post fragments of that code below.

AndroidManifest:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.gresanuemanuelvasi.test_wakeup">
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
        <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".ServiceStarter" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
                android:directBootAware="true" tools:targetApi="n">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </receiver>
        </application>
    </manifest>

    class ServiceStarter: BroadcastReceiver() {
        @SuppressLint("CommitPrefEdits")
        override fun onReceive(context: Context?, intent: Intent?) {
            Log.d("EMY_","Calling onReceive")
             context?.let {
                 Log.i("EMY_", "Received action: ${intent!!.getAction()}, user unlocked: " + UserManagerCompat.isUserUnlocked(context))

                 val sp =it.getSharedPreferences("EMY_", Context.MODE_PRIVATE)
                 sp.edit().putString(MainActivity.MY_KEY, "M-am activat asa cum trebuie!")

                 if (intent!!.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                     val i = Intent(it, MainActivity::class.java)
                     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                     it.startActivity(i)
                 }
            }
        }
    }

class MainActivity : AppCompatActivity() {

    companion object {
        const val MY_KEY="MY_KEY"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val kgm = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
        val kgl = kgm.newKeyguardLock(MainActivity::class.java.simpleName)
        if (kgm.inKeyguardRestrictedInputMode()) {
            kgl.disableKeyguard()
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(arrayOf(Manifest.permission.RECEIVE_BOOT_COMPLETED), 1234)
        }
        else
        {
            afisareRezultat()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {

        if(1234 == requestCode )
        {
            afisareRezultat()
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    private fun afisareRezultat() {
        Log.d("EMY_","Calling afisareRezultat")
        val sp = getSharedPreferences("EMY_", Context.MODE_PRIVATE);
        val raspuns = sp.getString(MY_KEY, "Doesn't exists")
        Log.d("EMY_", "AM primit: ${raspuns}")
        sp.edit().remove(MY_KEY).apply()
    }
}

Building on @Bhaskar's answer and others, here's a full command to unlock (tested on Pixel 3):

adb shell input keyevent 26 && adb shell input keyevent 82 && adb shell input text <password> && adb shell input keyevent 66

if the device is locked with black screen run the following:

  1. adb shell input keyevent 26 - this will turn screen on
  2. adb shell input keyevent 82 - this will unlock and ask for pin
  3. adb shell input text xxxx && adb shell input keyevent 66 - this will input your pin and press enter, unlocking device to home screen

Slightly modifying answer by @Yogeesh Seralathan. His answer works perfectly, just run these commands at once.

adb shell input keyevent 26 && adb shell input touchscreen swipe 930 880 930 380 && adb shell input text XXXX && adb shell input keyevent 66