javaandroidandroid-activitypermissions

How to enforce the custom permission on an Activity in Android?


I have created a custom permission in android as:

<permission
    android:name="com.master.me.CUSTOM_PERMISSION_TEST"
    android:description="@string/des_permission"
    android:label="labelhere">
</permission>

How will I enforce this in my Activity in AndroidManifest.xml file?


Solution

  • Use android:permission attribute into activity tag.

    Like below

    <activity android:permission="com.master.me.CUSTOM_PERMISSION_TEST"
                android:name=".YourActivity"
                android:label="@string/activity_label" />
    

    And you need to add uses-permission to your custom permission, when your other application needs to launch this activity.

    <uses-permission android:name="com.master.me.CUSTOM_PERMISSION_TEST"/>
    

    Android Permissions: Evolution, Attacks, and Best Practices is a very good article to understand permissions in Android. And How to use custom permissions in Android? is also a very good SO thread.