Intent custom permission not working

2019-02-15 16:17发布

问题:

UPDATE

My immediate problem is solved as per my answer below. My environment (PC + devices) does not allow custom permissions in DEBUG builds.

I'm convinced I was able to run DEBUG builds fine a few weeks ago - hence the description of an "intermittent" error.

UPDATE 2

The code synced out on a different developer's machine, works fine on his device in DEBUG mode through Eclipse. On my devices, it does not work from his machine.

Also the DEBUG build from my machine works fine on this other device.

This leads me to question if something has gone wrong on my devices?


I have an intermittent "Permission Denial" problem with custom permission Intents. It's one of those where the code is in production and works, but sometimes when making new builds, it stops working. I have only managed to fix it by luck before, through a process of reboot (device & pc), clean project, uninstall/reinstall, repeat etc... this is unacceptable.

As far as I can see I am doing everything correct for custom permissions on my Intents that I am broadcasting within my app. But logcat is giving me the usual errors of:

  • W/BroadcastQueue( 389): Permission Denial: broadcasting Intent { act=A_ACTION flg=0x10 } from com.test (pid=22030, uid=10002) requires A_PERMISSION due to registered receiver BroadcastFilter{43918d50 u0 ReceiverList{4391cad8 22030 com.test/10002/u0 remote:430576d0}}

on 4.2.1 (Galaxy Nexus) & 4.2.2 (Nexus 7), or

  • W/ActivityManager( 2002): Permission Denial: receiving Intent { act=A_ACTION flg=0x10 } to ProcessRecord{41a12748 31496:com.test/10115} (pid=31496, uid=10115) requires A_PERMISSION due to sender com.test (uid 10115)

on 4.0 (S2) & 2.3.3 (Samsung Ace).

i.e. standard errors.


My MANIFEST says:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test"
    android:versionCode="1"
    android:versionName="4.10.0.3" >

    <permission
        android:name="A_PERMISSION"
        android:description="@string/broadcast_permission_desc"
        android:label="@string/broadcast_permission_label"
        android:permissionGroup="@string/broadcast_permission_group"
        android:protectionLevel="signature" />

    <uses-permission android:name="A_PERMISSION" />

    etc.....

Those STRINGS are defined as (fwiw):

<string name="broadcast_permission_label">Private Broadcast</string>
<string name="broadcast_permission_desc">This permission allows the necessary components of the application to receive private broadcasts that are sent within the application.</string>
<string name="broadcast_permission_name">A_PERMISSION</string>
<string name="broadcast_permission_group">A_GROUP</string>

I have a method in my base APPLICATION subclass that I use to do the broadcasting:

public void sendBroadcast(Intent i)
{
    // hardcoded custom permission
    super.sendBroadcast(i, "A_PERMISSION");
}

I REGISTER for my BroadcastReceiver from within a Fragment like this:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // hardcoded custom permission
    MyApp.getInstance().registerReceiver(myReceiver, myFilter,
        "A_PERMISSION", null);
}

I have checked the following related questions without joy:

  • Android Permission Denial: broadcasting Intent
  • How to use custom permissions in Android?
  • how to declare user defined permissions in android

I think that covers similar questions on this site.

My problem is that its intermittent but wastes lots of time i.e. some builds work on all phones, some builds fail on all phones.

I wonder if this is an issue in one of the frameworks (either Android OS, or perhaps Eclipse / Android SDK). Unfortunately my googling only leads me to the more common issues with the common solutions.

回答1:

Final Solution - I agree it doesn't seem to make sense, but here it is:

I have 2 apps using the same permission - com.test and com.runtest.

As above, com.test was broken for DEBUG builds. But I noticed that com.runtest was still working.


Steps:

  1. installed com.runtest to confirm it was working
  2. uninstalled both apps from my devices
  3. renamed com.runtest to com.test - in Eclipse (Android Tools... Rename Application Package)
  4. debugged this renamed app in Eclipse on my device - it worked
  5. deleted this renamed "fake" com.test
  6. debugged the original com.test app in Eclipse on my device - it finally worked!

So for some reason, overwriting the broken app with a working app changed something, and fixed my device.