8

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??

srv_sud
  • 647
  • 1
  • 9
  • 26

4 Answers4

4

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
y4n9b0
  • 1,899
  • 1
  • 7
  • 6
  • 1
    I will plus 1 this when you put quotation marks around the name of the permission - then it works! – Janin Nov 02 '19 at 01:05
2

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
Community
  • 1
  • 1
Swing
  • 858
  • 1
  • 8
  • 21
  • I'd like to know how you do *1.* – tynn Feb 25 '16 at 08:40
  • 4
    And *2.* sadly yields `Operation not allowed: java.lang.SecurityException: Package com.android.shell has not requested com.myexample.permission.MY_PERMISSION`. – tynn Feb 28 '16 at 11:26
0

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.

Community
  • 1
  • 1
Alex Townsend
  • 1,564
  • 10
  • 13
  • Thanks for reply. I have already reffered to this post. But my requirement is to such that I need to have to use the permission. I would have done this with sample app. but for some test environment I need it to it through adb shell command scripts. – srv_sud Feb 24 '16 at 14:55
  • Take a look at the 2nd link in my edited response. I've never personally used it, but might be able to help you. – Alex Townsend Feb 24 '16 at 15:15
  • 1
    I am able to send broadcast using the the app. But my problem is that i need to send it using adb shell commands and permission I dont want to remove, this is my requirement. – srv_sud Feb 24 '16 at 15:23
0

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.

c0nstruct0r
  • 186
  • 1
  • 6