Android 系统监听切换用户事件

try{                                                                                                                                                                           
        ActivityManagerNative.getDefault().registerUserSwitchObserver(
            new IUserSwitchObserver.Stub() {
              @Override
              public void onUserSwitching(int newUserId, IRemoteCallback reply) {
                switchUser(newUserId, reply);//Here, you can do something you want
              }
         
              @Override
              public void onUserSwitchComplete(int newUserId) throws RemoteException {
              }
            });  
      } catch (RemoteException e) { 
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
}

ref : frameworks/base/services/java/com/android/server/WallpaperManagerService.java : 519