android 5.0 創建多用戶 雙開多開應用(1)


Andriod5.0多用戶 雙開應用


android多用戶是5.0之后有的,類似windows的賬戶系統


不過官方還沒有完全確認,API大都是hide狀態


我這里提供一種方式並不適用所有的,由於我們有定制化手機,所以有定制化的服務可以開發,所以只需要將源碼平台化編譯一把,將所需要的類抽取出來,打成jar,再通過AIDL方式暴露出相應的接口,當然這個服務也是系統服務。我們再去開發只需要調用相應AIDL提供相應的接口即可。

下面來詳細的說明:

1.首先系統服務

android:sharedUserId="android.uid.system"


簽名的時候需要平台對應的簽名文件需要signapk.jar簽名之后生成apk安裝即可


2.將需要的類打成jar(5.0以上版本編譯后的class)

out/target/common/obj/Java_Libraries/framework_intermediates..找到相應的對應的class


源文件

package android.os;

import android.os.Bundle;
import android.content.pm.UserInfo;
import android.content.RestrictionEntry;
import android.graphics.Bitmap;

/**
 *  {@hide}
 */
interface IUserManager {
    UserInfo createUser(in String name, int flags);
    UserInfo createProfileForUser(in String name, int flags, int userHandle);
    void setUserEnabled(int userHandle);
    boolean removeUser(int userHandle);
    void setUserName(int userHandle, String name);
    void setUserIcon(int userHandle, in Bitmap icon);
    Bitmap getUserIcon(int userHandle);
    List<UserInfo> getUsers(boolean excludeDying);
    List<UserInfo> getProfiles(int userHandle, boolean enabledOnly);
    UserInfo getProfileParent(int userHandle);
    UserInfo getUserInfo(int userHandle);
    boolean isRestricted();
    int getUserSerialNumber(int userHandle);
    int getUserHandle(int userSerialNumber);
    Bundle getUserRestrictions(int userHandle);
    boolean hasUserRestriction(in String restrictionKey, int userHandle);
    void setUserRestrictions(in Bundle restrictions, int userHandle);
    void setApplicationRestrictions(in String packageName, in Bundle restrictions,
            int userHandle);
    Bundle getApplicationRestrictions(in String packageName);
    Bundle getApplicationRestrictionsForUser(in String packageName, int userHandle);
    boolean setRestrictionsChallenge(in String newPin);
    int checkRestrictionsChallenge(in String pin);
    boolean hasRestrictionsChallenge();
    void removeRestrictions();
    void setDefaultGuestRestrictions(in Bundle restrictions);
    Bundle getDefaultGuestRestrictions();
    boolean markGuestForDeletion(int userHandle);
}


3.寫好相應的方法調用:

 

 /** 
     * 得到 IUserManager 
     * @return IUserManager 
     */  
    private IUserManager getIUserManager(){  
        IUserManager iUserManager = null;  
        IBinder binder = null;  
                try {  
                    if(iUserManager==null){  
                        Class<?> ServiceManagerClass = Class.forName("android.os.ServiceManager");  
                        Method method = ServiceManagerClass.getMethod("getService", String.class);  
                        if(binder==null){  
                            binder = (IBinder) method.invoke(ServiceManagerClass, Context.USER_SERVICE);  
                        }  
                        iUserManager = IUserManager.Stub.asInterface(binder);  
                        return iUserManager;  
                    }  
                } catch (Exception e) {  
                        e.printStackTrace();  
                }  
            return null;          
    }  

 

 

public UserInfo createUser(String name, int flags){
                    UserInfo info = null;
                    IUserManager iUserManager = getIUserManager();
                    if(info == null){
                        try {
                            info = iUserManager.createUser(name, flags);
                             loguser(iUserManager);
                            return info;
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                return null;
    }

 

   public UserInfo getUserInfo(int userHandle){
        UserInfo info = null;
        IUserManager iUserManager = getIUserManager();
        if(info == null){
            try {
                info = iUserManager.getUserInfo(userHandle);
                 loguser(iUserManager);
                return info;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

 

 public boolean removeUser(int userHandle){
        IUserManager iUserManager = getIUserManager();
        boolean isremove = false;
        try {
            isremove = iUserManager.removeUser(userHandle);
             loguser(iUserManager);
            return isremove;
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }

 4寫一個AIDL我這里是通過回調方式寫的,不熟悉的同學可以回去看看AIDL

   case SERVICE_EXTENDED_API_CREATE_USER_MODE:{  
                //TODO  
                if(para!=null){  
                 String name = para.getString(KEY_USER_NAME);  
                 int flags = para.getInt(KEY_USER_FLAG);  
                 UserInfo info = muserPolicy.createUser(name, flags);  
                 if(info!=null){  
                         if (cb != null) {  
                            Bundle data = new Bundle(1);  
                            data.putParcelable(KEY_USER_INFO, info);  
                            try {  
                                cb.onResultOfCallExtendedApi(SERVICE_EXTENDED_API_CREATE_USER_MODE, data);  
                            } catch (RemoteException e) {  
                                e.printStackTrace();  
                            }  
                        }  
                 }  
                }  
                 return 0;  
            }  
  case SERVICE_EXTENDED_API_GET_USER_INFO:{  
                if(para!=null){  
                    int userHandle = para.getInt(KEY_USER_HANDLER);  
                    UserInfo info = muserPolicy.getUserInfo(userHandle);  
                    if(info !=null){  
                         if (cb != null) {  
                             Bundle data = new Bundle(1);  
                             data.putParcelable(KEY_USER_INFO, info);  
                             try {  
                                 cb.onResultOfCallExtendedApi(SERVICE_EXTENDED_API_GET_USER_INFO, data);  
                             } catch (RemoteException e) {  
                                 e.printStackTrace();  
                             }  
                         }  
                    }  
                }  
                return 0;  
            }  

 

  case SERVICE_EXTENDED_API_REMOVE_USER:{  
                if(para!=null){  
                    int userHandle = para.getInt(KEY_USER_HANDLER);  
                    boolean isremove = muserPolicy.removeUser(userHandle);  
                     if (cb != null) {  
                         Bundle data = new Bundle(1);  
                         data.putBoolean(KEY_USER_IS_REMOVE, isremove);  
                         try {  
                             cb.onResultOfCallExtendedApi(SERVICE_EXTENDED_API_REMOVE_USER, data);  
                         } catch (RemoteException e) {  
                             e.printStackTrace();  
                         }  
                     }  
                }  
                return 0;  
            }  

5.再在需要的應用上調用服務端相應的接口:

 

啟動的時候需要bind系統服務

 

    private void init(){  
            Intent intent1 = new Intent();  
            intent1.setPackage("com.xxx.xxx");  
            intent1.setAction("com.xxx.xxxService");  
            bindService(intent1, conn1, Context.BIND_AUTO_CREATE);  
        }  
          
          
    case R.id.btn_create:  
                Bundle create = new Bundle();  
                create.putString("username", "Zeng");  
                try {  
                    if(ips!=null){  
                        ips.callExtendedApi(22, create, ipsc);  
                    }else{  
                        Log.e("peng","onclickcreate serviceisnull");  
                    }  
                      
                } catch (RemoteException e) {  
                    e.printStackTrace();  
                }  
                break;  
            case R.id.btn_get:  
                Bundle get = new Bundle();  
                get.putInt("userHandle", mhandle);  
                try {  
                    if(ips!=null){  
                        ips.callExtendedApi(23, get, ipsc);  
                    }else{  
                        Log.e("peng","onclickget serviceisnull");  
                    }  
                      
                } catch (RemoteException e) {  
                    e.printStackTrace();  
                }  
                break;  
            case R.id.btn_remove:  
                int i = Integer.parseInt(et_text.getText().toString().trim());  
                Bundle remove = new Bundle();  
                remove.putInt("userHandle", i);  
                try {  
                    if(ips!=null){  
                        ips.callExtendedApi(24, remove, ipsc);  
                    }else{  
                        Log.e("peng","onclickremove serviceisnull");  
                    }  
                      
                } catch (RemoteException e) {  
                    e.printStackTrace();  
                }  
                break;  

 

 

對應的callback可以返回對應的數據。


 

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com