22

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?

Master
  • 2,945
  • 5
  • 34
  • 65
  • 1
    possible duplicate of [How to use custom permissions in Android?](http://stackoverflow.com/questions/8816623/how-to-use-custom-permissions-in-android) – Master Mar 04 '14 at 15:40
  • where we must enforce custom permission on an activity ? – Ajay S Apr 28 '16 at 16:18

1 Answers1

46

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"/>

An In-Depth Introduction to the Android Permission Model is a very good article to understand permission in Android. And How to use custom permissions in Android? is also a very good SO thread.

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • @PankajKumar: Just having a simple doubt: Does using CUSTOM_PERMISSION_TEST in an activity means that any other application which does not have this permission(CUSTOM_PERMISSION_TEST), will not be able to start this activity? – Zax Nov 22 '16 at 13:21
  • @Zax Yes you are perfect. – Pankaj Kumar Nov 22 '16 at 14:21
  • 3
    @Zax And I want to add one more thing. If you add protectionLevel="signature" while defining custom permission, then even if another application declares your permission, can not access that Activity until that is signed by same certificate. Read more at https://developer.android.com/guide/topics/manifest/permission-element.html#plevel – Pankaj Kumar Nov 22 '16 at 14:21