You can run an ADB command after clicking a Google Maps link on Windows by creating a custom URL protocol handler. This involves registering a specific URI scheme in the Windows Registry that points to a batch file or script. When you click a map link using this custom scheme, Windows executes the associated script, which then triggers the desired Android Debug Bridge action on your connected device.
Understanding the Mechanism
Google Maps links typically use the
https:// or
http:// protocols. To intercept these clicks and trigger an ADB command, you cannot modify Google's server-side output. Instead, you must create a client-side bridge. The most reliable method is defining a custom protocol, such as
mapsadb://. When a user clicks a hyperlink with this scheme, the operating system looks up the registered handler and executes the specified program. This approach decouples the web interaction from the device management task, allowing for precise control over when and how the ADB command runs.
This technique is particularly useful for automation workflows where you want to send location data or launch specific intents on an Android device directly from a browser session. It avoids the need for manual command-line entry every time a new map coordinate is identified. By automating this step, you reduce latency between identifying a target location and executing actions on the device.
Creating the Execution Script
The first step is to create a script that Windows will execute. A batch file is the simplest solution for Windows environments. Create a new text file and name it
handle_maps.bat. Inside this file, you need to parse the arguments passed by the URL and translate them into an ADB command.
Open the file in a text editor and add the following logic:
@echo off
REM Extract the query string from the URL argument
set "url=%~1"
REM Remove the protocol prefix mapsadb://
set "data=%url:mapsadb://=%"
REM Example: Parse latitude and longitude if present
REM For simplicity, this example just sends a generic toast or intent
REM You would replace this line with your actual ADB command
adb shell am start -a android.intent.action.VIEW -d "%data%"
REM Optional: Pause to see output
pause
Ensure that the path to your
adb.exe is either in your system's PATH environment variable or specify the full path in the script (e.g.,
C:\Users\YourName\AppData\Local\Android\Sdk\platform-tools\adb.exe). If you encounter issues with spaces in paths, refer to our guide on
Batch File Not Working with Space in File Name to properly escape characters.
Save the file in a secure directory, such as
C:\ADB_Handlers\. Test the script manually by opening a command prompt and running
handle_maps.bat mapsadb://example.com to verify it launches correctly without errors.
Registering the Protocol in Windows Registry
Once the script is ready, you must tell Windows how to handle the
mapsadb:// scheme. This is done via the Windows Registry. You can create a
.reg file to automate this process safely.
Create a new text file named
register_protocol.reg and paste the following content, adjusting the path to your batch file as needed:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\mapsadb]
@="URL:MapsADB Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\mapsadb\DefaultIcon]
@="C:\\ADB_Handlers\\handle_maps.bat,1"
[HKEY_CLASSES_ROOT\mapsadb\shell]
[HKEY_CLASSES_ROOT\mapsadb\shell\open]
[HKEY_CLASSES_ROOT\mapsadb\shell\open\command]
@="\"C:\\ADB_Handlers\\handle_maps.bat\" \"%1\""
Note the double backslashes in the file path; this is required for registry syntax. Double-click the
.reg file and accept the prompts to merge it into the registry. After doing so, Windows associates the
mapsadb scheme with your batch file.
If you have previously set a different default application for unknown protocols, you may need to clear your browser's site settings for permissions. In Chrome, go to Settings > Privacy and security > Site Settings > Additional permissions > Custom handlers. Ensure that the new handler is recognized.
Testing the Integration
To verify the setup, open any web browser and navigate to a page containing a link formatted as
<a href="mapsadb://com.google.android.apps.maps">Launch Maps</a>. Click the link. Windows should display a security prompt asking which app to use. Select your batch file or the default handler if configured correctly.
The script should execute, and the ADB command defined within it should run on your connected device. Check the ADB logcat or the device screen to confirm the action took place. If nothing happens, check the Event Viewer in Windows under Applications logs for errors related to the batch file execution. Common issues include incorrect file paths, missing quotes around arguments, or ADB not being able to communicate with the device due to permission issues.
For users who find themselves needing to manage other system-level interactions, understanding how to isolate processes can be helpful. While this topic focuses on ADB, similar principles apply to managing audio outputs, as discussed in
How to Disable Sound from a Windows Application, ensuring your debugging sessions remain uninterrupted by background noise.
Frequently Asked Questions
Can I use PowerShell instead of a batch file?
Yes, you can register a PowerShell script as the handler. Change the command in the registry to point to
powershell.exe -File "C:\path\to\script.ps1" %1. Ensure your execution policy allows running local scripts, or use the
-ExecutionPolicy Bypass flag in the command line.
Why does the ADB command fail even though the script runs?
This usually happens because the script runs in a context where the
adb executable is not in the PATH. Always use the absolute path to
adb.exe in your script. Additionally, ensure your device is connected and authorized via USB debugging before clicking the link.
Is it safe to register custom URL protocols?
It is generally safe as long as you trust the source of the registry changes. Custom protocols do not grant elevated privileges by default. However, avoid registering schemes that mimic official system protocols (like
file:// or
http://) to prevent conflicts or security vulnerabilities.
How do I remove the custom protocol handler?
To remove the handler, delete the
HKEY_CLASSES_ROOT\mapsadb key from the Registry Editor. You can also create a
.reg file with
- before the key path (e.g.,
[-HKEY_CLASSES_ROOT\mapsadb]) and merge it to delete the entries cleanly.
Can I pass complex JSON data through the URL?
Standard URLs have length limits and character restrictions. For complex data, consider encoding the payload using Base64 or passing a reference ID that the script fetches from a local database or API. Avoid putting raw JSON directly in the URL query string to prevent parsing errors in the batch file.