How to manage restricted profiles in android app?

2020-02-24 04:29发布

问题:

Restricted profile are now available in android from 4.3, i came to know that some apps like camera, gmail etc are not available in these kind of profiles. How can i manage these type of conditions in my app? Also how to manage in app products and restricted profile settings in corresponding to my app?

回答1:

Thanks user370305 even if i already visited

http://developer.android.com/about/versions/android-4.3.html#RestrictedProfiles

I would like to improve it from reference with

https://www.youtube.com/watch?v=pdUcANNm72o

Restricted Profiles are a new feature introduced in Android Jelly Bean 4.3 that enables you to give users of your applications improved control when sharing their tablet.

These Restricted Profiles share apps, google account of primary user account but in a restricted manner.They dont get access to gmail, play store, calender etc. Primary user can select the restrictions for each applications.

UserManager Class is extended for managing these restrictions

UserManager.getUserRestrictions returns the user-wide restrictions imposed on the user specified

UserManager.getApplicationRestrictions returns a bundle containing any saved application restrictions for this user, for the given package name. Only an application with this package name can call this method.

If you need specific settings use this intent filter

<receiver android:name="GetRestrictionsReceiver">
<intent-filter>
<action android:name="android.intent.action.GET_RESTRICTION_ENTRIES "/>
</intent-filter>
</receiver>

now implement broadcast receiver with Restriction Entry list returned like this

public  class GetRestrictionsReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            final PendingResult result=goAsync();
            new Thread(){
                public void run(){
                    final Bundle extras=new Bundle();
                    ArrayList<RestrictionEntry> newEntries = initRestricions(context);
                    extras.putParcelableArrayList(Intent.EXTRA_RESTRICTIONS_LIST, newEntries);

                    result.setResult(Activity.RESULT_OK, null, extras);
                    result.finish();
                }

            }.start();

        }


    }

RestrictionEntry

Any application that chooses to expose such restrictions does so by implementing a receiver that handles theACTION_GET_RESTRICTION_ENTRIES action. The receiver then returns a result bundle that contains an entry called "restrictions", whose value is an ArrayList.

There are 3 types of Restriction Entry

  1. Boolean
  2. Single Choice
  3. Multiple Choice

You can use different methods of RestrictionEntry to set and get different type of restrictions.

To get access to an account from a restricted profile, you must add the android:restrictedAccountType attribute to the tag:

<application ...
    android:restrictedAccountType="com.example.account.type" >


回答2:

The UI for users to control the restrictions you've built is managed by the system's Settings application. To make your app's restriction settings appear to the user, you must declare the restrictions your app provides by creating a BroadcastReceiver that receives the ACTION_GET_RESTRICTION_ENTRIES intent. The system invokes this intent to query all apps for available restrictions, then builds the UI to allow the primary user to manage restrictions for each restricted profile.

For more info look at http://developer.android.com/about/versions/android-4.3.html#RestrictedProfiles



回答3:

you can use following code section to detect if your app is running in restricted profile mode !!

import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;

public static boolean isRestrictedProfileInEffect(Context context) {
  UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
  UserInfo restrictedUserInfo = null;
  for (UserInfo userInfo : userManager.getUsers()) {
        if (userInfo.isRestricted()) {
            restrictedUserInfo = userInfo;
        }
    }
  boolean isOwner = UserHandle.myUserId() == UserHandle.USER_OWNER;
  boolean isRestrictedProfileOn = restrictedUserInfo != null && !isOwner;
  return isRestrictedProfileOn;
}