How can a shell script get the number of an incoming/outgoing call?


Question

new user here from Montréal, Canada!



So i have a shell script that runs when a call comes in, and another one that runs when i make a call. How can this script get the phone number of the caller / of the number being called? Grepping through the logcat output doesn't show it.


Answer

Receiving caller number from incoming call



Tasker maintains a global variable named Call Number (%CNUM) which would reflect caller number for the incoming call.



If you intend to run your script when the device rings, you may setup the following profile:




  • Profile: Event → Phone → Phone Ringing → Priority: Highest

  • Task: (Actions): Code → Run Shell → Command: sh FILE_PATH "%CNUM"



    Replace FILE_PATH with the absolute file path of your script. In your script, %CNUM can be received as an argument by storing $1 into a variable. (See this answer by Bruce Ediger for any related help.)



    Example script:



    #!/system/bin/sh
    incoming_no="$1"
    echo "$incoming_no" >> /sdcard/incoming_no.txt


    Save the script and try executing it from an action in that profile. For an incoming call, it would create or append the file incoming_no.txt with the caller number.




Note: Tasker supports creating global variables, so if you don't intend to run your script with that profile, use the action Variable → Variable Set to store the value of %CNUM into a user specific global variable. Whenever time is ripe, use that variable in your script from Tasker.



Receiving called number from outgoing call



Tasker doesn't have an inbuilt support for this, as it seems to me. Worry not, since this answer by sush can be used in Tasker for our goal. The crux of that answer is: listen to a broadcast with action android.intent.action.NEW_OUTGOING_CALL and receive the value from a particular extra type key.



Tasker has support for receiving intents. Before we begin with profile, hear me out on how I extracted the value.



I made a call to a number and immediately ran the following command (requires in PC):



adb shell dumpsys activity


In the output, under the section ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts) I noticed:




BroadcastRecord{ 32699300 u0 android.intent.action.NEW_OUTGOING_CALL }
act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000030 (has extras)
extras: Bundle[{ android.intent.extra.PHONE_NUMBER=+919XXXXYYYZZ } ]


Here, android.intent.extra.PHONE_NUMBER is the extra type key.



As for Tasker, setup your profile in the following fashion:




  • Profile: Event → System → Intent Received:




    • Action: android.intent.action.NEW_OUTGOING_CALL

    • Priority: Highest


  • Task: (Actions):
    the number can be retrieved from the variable



    %android_intent_extra_phone_number 


    As usual, store the value of that variable into a user specific global variable using Variable → Variable Set and use the latter in your script whenever you want.




Note:




  • The solution is tested on Android 4.2.1 and Android 5.0.2 (COS12).

  • Per userguide of Tasker, the Variables tab is not shown if Menu / Prefs / UI / Beginner Mode is enabled.

  • Caller number for incoming calls can alternatively be retrieved by listening the broadcast intent with action android.intent.action.PHONE_STATE and receiving the number from %incoming_number. Use %state in a condition to check whether its value is RINGING or IDLE. If former, get the value from %incoming_number.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes