Restricted Profiles Settings

Yesterday Netflix for Android introduced the Restricted Profiles feature.

It is a very powerful opportunity, but very few apps are using this feature.
Remember that restricted profiles require API 18.

Let's see how to realize it.

It is very simple.
First of all, we need to use a BroadcastReceiver that receives the ACTION_GET_RESTRICTION_ENTRIES intent.

        <action android:name="android.intent.action.GET_RESTRICTION_ENTRIES" />
When we click on the setting icon, our BroadcastReceiver is called.
Our receiver has a EXTRA_RESTRICTIONS_INTENT Bundle which contains the key-value pairs for each restriction.
    public void onReceive(final Context context, Intent intent) {

          final Bundle currentRestrictions =
In onReceive() method you have to create a RestrictionEntry for each restriction your app provides.

Each RestrictionEntry defines a restriction title, description, and one of the following data types:
An example for a boolean value:
    RestrictionEntry entry1 = new RestrictionEntry(KEY_BOOLEAN, true);  //default value!
If you want to set the current value you can use something like this::
    //Retrieve current value 
    if (currentRestrictions != null) {
         entry1.setSelectedState(currentRestrictions.getBoolean(KEY_BOOLEAN, true));

An example for a multiple choices that are mutually exclusive:
    RestrictionEntry entry2 = new RestrictionEntry(KEY_CHOICE, (String) null);
    entry2.setChoiceEntries(context, R.array.choice_entries);

    Resources res = context.getResources();
    String[] values = res.getStringArray(R.array.choice_values);
If you want to set the current value you can use something like this:
    //Retrieve current value
    if (currentRestrictions != null) {
         if (currentRestrictions.containsKey(KEY_CHOICE)) {
         } else {
              entry2.setSelectedString(values[0]); //Default

An example for a multiple choices that are not mutually exclusive:
     RestrictionEntry entry3 = new RestrictionEntry(KEY_MULTI_SELECT, (String[]) null);
     entry3.setChoiceEntries(context, R.array.choice_entries);

If you want to set the current value you can use something like this:
     String[] selected= new String[1];
     selected[0]= values[0];
     //Retrieve current value
     if (currentRestrictions != null) {
          if (currentRestrictions.containsKey(KEY_MULTI_SELECT)) {
          } else {
               entry3.setAllSelectedStrings(selected); //Default

Finally you have to put all the RestrictionEntry objects into an ArrayList and put it into the broadcast receiver's result as the value for the EXTRA_RESTRICTIONS_LIST extra.
    public void onReceive(final Context context, Intent intent) {

        final PendingResult result = goAsync();

        final Bundle currentRestrictions =

        new Thread() {
            public void run() {
                ArrayList list = initRestrictionEntries(context, currentRestrictions);

                Bundle extras = new Bundle();
                extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, list);
                result.setResult(Activity.RESULT_OK, null, extras);

Remember that onReceive method will run in the main activity thread, so it is useful to use a Thread to read and set the values.

The last step is to read this value from your app by calling the getApplicationRestrictions method.
   Bundle mRestrictionsBundle =
         ((UserManager) getSystemService(Context.USER_SERVICE))
Pay attention: restrictions are defined for each restricted profile.So when you open your app, you are reading the current restrictions for the current profile.
If the app is running in the main profile, you will receive an empty Bundle.

   boolean value=mRestrictionsBundle.getBoolean(MyRestrictedProfileBroadcast.KEY_BOOLEAN);
   String choice=(mRestrictionsBundle.getString(MyRestrictedProfileBroadcast.KEY_CHOICE);
   String[] values = mRestrictionsBundle.getStringArray

You can get code from GitHub:

Popular posts from this blog

Expand and collapse animation

How to centralize the support libraries dependencies in gradle

A Material styled AlertDialog