How to manage restricted profiles in android app?

最后都变了- 提交于 2019-11-30 05:23:38

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

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!