Figuring Out Your Own Phone Number Programmatically on A Rooted Android Handset

You can find out your own phone number on your Android handset by looking at Settings -> Device Info -> Status. Doing it programmatically, though, is not a trivial task, but the following method works if your handset is rooted.

The command string in Automate. Notice the left curly brace is quoted.

The command string in Automate. Notice the left curly brace is quoted.

On my Samsung Galaxy S5 SCL23 (the Japanese carrier Au’s version), the following script works (with the root privilege invoked). The presence of a Busybox binary is assumed because applets ‘awk’ and ‘tr’ are used.

#service call iphonesubinfo 8 | awk -F\' '{print $2}' | tr -d '.\n'

This is based on “How to get the phone number of an android phone via adb? – Stack Overflow.” According to this article, the number “8” above depends on your Android version. The article then references “Public scratchpad: Calling Android services from ADB shell” for a way to automatically figure it out from a *nix box—but I did not go that far.

The command string is converted by the cliEncode function before being executed.

The command string is converted by the cliEncode function before being executed.

I wanted to execute this script from Automate—a Tasker-like automation app for Android from Llamalab (Automate on Google Play). Special care had to be taken about the literal text string for this command because in Automate, curly braces are special characters and used to signify in-place text substitution (“interpolation”) in literal text strings, so left curly braces have to be quoted if otherwise (see pic).

Before executing this script in a Shell command Superuser block, I have it converted by the ‘cliEncode’ function just to be on the safe side (see pic).

広告

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中