srv_sud
srv_sud

Reputation: 659

How to send broadcast with permission from command line

I have a broadcast receiver in a app which uses my permission com.myexample.permission.MY_PERMISSION.

I am able to send the broadcast using

sendBroadcast(intent, "com.myexample.permission.MY_PERMISSION")

from activities.

To send a broadcast for action as com.com.myexample.MY_ACTION and extra data with key as MY_EXTRA below command can be used:

$adb shell am broadcast -a com.com.myexample.MY_ACTION
--ez MY_EXTRA true

But how to send broadcast with permission com.myexample.permission.MY_PERMISSION to the app from command line using adb shell commands??

Upvotes: 9

Views: 15108

Answers (4)

c0nstruct0r
c0nstruct0r

Reputation: 196

I ended up creating a BroadcastingActivity that would receive the intent sent by adb and broadcasted it with the correct permissions. In this way the broadcast itself is consistent between debug/release and the BroadcastingActivity can be moved to the debug build type.

Upvotes: 0

y4n9b0
y4n9b0

Reputation: 1939

Use arg --receiver-permission which belongs to subcmd broadcast:

adb shell am broadcast -a com.com.myexample.MY_ACTION --receiver-permission com.myexample.permission.MY_PERMISSION

Upvotes: 5

Swing
Swing

Reputation: 926

If my answer here is not what you are looking for, try pm grant PACKAGE PERMISSION:

  1. add "development" level when define com.myexample.permission.MY_PERMISSION in your app, like android:protectionLevel="signature|development"
  2. grant this permission to "shell" in adb shell:
    pm grant com.android.shell com.myexample.permission.MY_PERMISSION

Upvotes: 2

Alex Townsend
Alex Townsend

Reputation: 1564

Taking a look at this post try removing the permission temporarily when you're doing testing from adb, and then re-adding the permission when you're ready to test it through a real broadcast.

If that is not acceptable, this post may be of some help.

Upvotes: 0

Related Questions