1

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.

5
  • How do you run your shell script? MacroDroid can give you the number for both incoming and outgoing calls. You can use that app with Secure Settings to pass the number directly to your shell script or let it write a file and read the file from your shell script. // Let me know if this would suffice as an answer to you.
    – Firelord
    Commented Feb 16, 2016 at 10:27
  • Which Android version and device are you using? Is it rooted?
    – Firelord
    Commented Feb 16, 2016 at 10:46
  • Thanks very much for your reply Firelord. I (currently) use Tasker to run this script. It works with MacroDroid dumping the info to a log file, and then retrieving it from my script, but i wish there was a less clunky way to do it. I could use MacroDroid directly if it had the ability to run arbitrary shell commands, i haven't found how to do that though :/
    – delt
    Commented Feb 16, 2016 at 17:18
  • Android version is 4.4.2 running on a Samsung Galaxy S3.
    – delt
    Commented Feb 16, 2016 at 17:19
  • Oh forgot to mention, yeah it's rooted.
    – delt
    Commented Feb 16, 2016 at 19:54

1 Answer 1

0

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.
2
  • Wow, thanks very much Firelord for the very detailed and complete answer!
    – delt
    Commented Feb 18, 2016 at 3:45
  • On a similar topic, is there a way for my script to know if the phone is ringing, or connected to a call?
    – delt
    Commented Feb 18, 2016 at 3:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .