android.os.Process.myUserHandle()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(107)

本文整理了Java中android.os.Process.myUserHandle()方法的一些代码示例,展示了Process.myUserHandle()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Process.myUserHandle()方法的具体详情如下:
包路径:android.os.Process
类名称:Process
方法名:myUserHandle

Process.myUserHandle介绍

暂无

代码示例

代码示例来源:origin: robolectric/robolectric

/** Adds {@code userHandle} to the list of accessible handles. */
public void addTargetUserProfile(UserHandle userHandle) {
 if (userHandle.equals(Process.myUserHandle())) {
  throw new IllegalArgumentException("Cannot target current user");
 }
 targetUserProfiles.add(userHandle);
}

代码示例来源:origin: robolectric/robolectric

/** Removes {@code userHandle} from the list of accessible handles, if present. */
public void removeTargetUserProfile(UserHandle userHandle) {
 if (userHandle.equals(Process.myUserHandle())) {
  throw new IllegalArgumentException("Cannot target current user");
 }
 targetUserProfiles.remove(userHandle);
}

代码示例来源:origin: robolectric/robolectric

@Implementation(minSdk = LOLLIPOP)
protected boolean setApplicationHidden(ComponentName admin, String packageName, boolean hidden) {
 enforceActiveAdmin(admin);
 if (packagesToFailForSetApplicationHidden.contains(packageName)) {
  return false;
 }
 if (hidden) {
  wasHiddenPackages.add(packageName);
 }
 return applicationPackageManager.setApplicationHiddenSettingAsUser(
   packageName, hidden, Process.myUserHandle());
}

代码示例来源:origin: robolectric/robolectric

@Implementation(minSdk = M)
public StatusBarNotification[] getActiveNotifications() {
 StatusBarNotification[] statusBarNotifications =
   new StatusBarNotification[notifications.size()];
 int i = 0;
 for (Map.Entry<Key, Notification> entry : notifications.entrySet()) {
  statusBarNotifications[i++] = new StatusBarNotification(
  RuntimeEnvironment.application.getPackageName(),
  null /* opPkg */,
  entry.getKey().id,
  entry.getKey().tag,
  android.os.Process.myUid() /* uid */,
  android.os.Process.myPid() /* initialPid */,
  0 /* score */,
  entry.getValue(),
  android.os.Process.myUserHandle(),
  0 /* postTime */);
 }
 return statusBarNotifications;
}

代码示例来源:origin: robolectric/robolectric

@Implementation(minSdk = LOLLIPOP)
protected boolean isApplicationHidden(ComponentName admin, String packageName) {
 enforceActiveAdmin(admin);
 return applicationPackageManager.getApplicationHiddenSettingAsUser(
   packageName, Process.myUserHandle());
}

代码示例来源:origin: robolectric/robolectric

@Implementation(minSdk = LOLLIPOP)
protected void clearUserRestriction(ComponentName admin, String key) {
 enforceActiveAdmin(admin);
 getShadowUserManager().setUserRestriction(Process.myUserHandle(), key, false);
}

代码示例来源:origin: robolectric/robolectric

@Implementation(minSdk = LOLLIPOP)
protected void addUserRestriction(ComponentName admin, String key) {
 enforceActiveAdmin(admin);
 getShadowUserManager().setUserRestriction(Process.myUserHandle(), key, true);
}

代码示例来源:origin: oasisfeng/condom

@Test public void testHiddenApi() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  final Object uid = PackageManager.class.getMethod("getUidForSharedUser", String.class).invoke(condom.getPackageManager(),"android.uid.system");
  assertEquals(1000, (int) uid);
  // This hidden API is used by some 3rd-party libraries, as reported in issue #9 on GitHub.
  if (SDK_INT >= LOLLIPOP) PackageManager.class.getMethod("getUserBadgeForDensity", UserHandle.class, int.class)
      .invoke(condom.getPackageManager(), Process.myUserHandle(), DisplayMetrics.DENSITY_DEFAULT);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = JELLY_BEAN_MR1)
public void sendBroadcastAsUser_sendBroadcast() throws IntentSender.SendIntentException {
 String action = "foo-action";
 Intent intent = new Intent(action);
 context.sendBroadcastAsUser(intent, Process.myUserHandle());
 assertThat(shadowOf(context).getBroadcastIntents().get(0).getAction()).isEqualTo(action);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = M)
public void testGrantRuntimePermission_packageNotFound() throws Exception {
 try {
  packageManager.grantRuntimePermission(
    "com.unknown.package", "android.permission.SEND_SMS", Process.myUserHandle());
  fail("Exception expected");
 } catch (SecurityException expected) {
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = LOLLIPOP)
public void shouldGetUserProfiles() {
 assertThat(userManager.getUserProfiles()).contains(Process.myUserHandle());
 UserHandle anotherProfile = newUserHandle(2);
 shadowOf(userManager).addUserProfile(anotherProfile);
 assertThat(userManager.getUserProfiles()).containsExactly(Process.myUserHandle(), anotherProfile);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = LOLLIPOP)
public void bindServiceAsUser() throws Exception {
 Intent serviceIntent = new Intent();
 ServiceConnection serviceConnection = buildServiceConnection();
 int flags = 0;
 assertThat(
     context.bindServiceAsUser(
       serviceIntent, serviceConnection, flags, Process.myUserHandle()))
   .isTrue();
 assertThat(shadowOf(context).getBoundServiceConnections()).hasSize(1);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(sdk = VERSION_CODES.P)
public void shouldReturnLocationEnabledOnceSet() {
 locationManager.setLocationEnabledForUser(true, Process.myUserHandle());
 assertTrue(locationManager.isLocationEnabled());
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = M)
public void testRevokeRuntimePermission() throws Exception {
 PackageInfo packageInfo = new PackageInfo();
 packageInfo.packageName = TEST_PACKAGE_NAME;
 packageInfo.requestedPermissions =
   new String[] {"android.permission.SEND_SMS", "android.permission.READ_SMS"};
 packageInfo.requestedPermissionsFlags =
   new int[] {REQUESTED_PERMISSION_GRANTED, REQUESTED_PERMISSION_GRANTED};
 shadowPackageManager.installPackage(packageInfo);
 packageManager.revokeRuntimePermission(
   TEST_PACKAGE_NAME, "android.permission.SEND_SMS", Process.myUserHandle());
 assertThat(packageInfo.requestedPermissionsFlags[0]).isEqualTo(0);
 assertThat(packageInfo.requestedPermissionsFlags[1]).isEqualTo(REQUESTED_PERMISSION_GRANTED);
 packageManager.revokeRuntimePermission(
   TEST_PACKAGE_NAME, "android.permission.READ_SMS", Process.myUserHandle());
 assertThat(packageInfo.requestedPermissionsFlags[0]).isEqualTo(0);
 assertThat(packageInfo.requestedPermissionsFlags[1]).isEqualTo(0);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = M)
public void testGrantRuntimePermission() throws Exception {
 PackageInfo packageInfo = new PackageInfo();
 packageInfo.packageName = TEST_PACKAGE_NAME;
 packageInfo.requestedPermissions =
   new String[] {"android.permission.SEND_SMS", "android.permission.READ_SMS"};
 packageInfo.requestedPermissionsFlags = new int[] {0, 0}; // Not granted by default
 shadowPackageManager.installPackage(packageInfo);
 packageManager.grantRuntimePermission(
   TEST_PACKAGE_NAME, "android.permission.SEND_SMS", Process.myUserHandle());
 assertThat(packageInfo.requestedPermissionsFlags[0]).isEqualTo(REQUESTED_PERMISSION_GRANTED);
 assertThat(packageInfo.requestedPermissionsFlags[1]).isEqualTo(0);
 packageManager.grantRuntimePermission(
   TEST_PACKAGE_NAME, "android.permission.READ_SMS", Process.myUserHandle());
 assertThat(packageInfo.requestedPermissionsFlags[0]).isEqualTo(REQUESTED_PERMISSION_GRANTED);
 assertThat(packageInfo.requestedPermissionsFlags[1]).isEqualTo(REQUESTED_PERMISSION_GRANTED);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = LOLLIPOP)
public void hasUserRestriction() {
 assertThat(userManager.hasUserRestriction(UserManager.ENSURE_VERIFY_APPS)).isFalse();
 UserHandle userHandle = Process.myUserHandle();
 shadowOf(userManager).setUserRestriction(userHandle, UserManager.ENSURE_VERIFY_APPS, true);
 assertThat(userManager.hasUserRestriction(UserManager.ENSURE_VERIFY_APPS)).isTrue();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void addTargetProfile_currentUserHandle_shouldThrowIllegalArgumentException() {
 assertThrows(
   IllegalArgumentException.class,
   () -> shadowOf(crossProfileApps).addTargetUserProfile(Process.myUserHandle()));
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = JELLY_BEAN_MR2)
public void clearUserRestrictions() {
 assertThat(userManager.getUserRestrictions().size()).isEqualTo(0);
 shadowOf(userManager)
   .setUserRestriction(Process.myUserHandle(), UserManager.ENSURE_VERIFY_APPS, true);
 assertThat(userManager.getUserRestrictions().size()).isEqualTo(1);
 shadowOf(userManager).clearUserRestrictions(Process.myUserHandle());
 assertThat(userManager.getUserRestrictions().size()).isEqualTo(0);
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = M)
public void testGrantRuntimePermission_doesntRequestPermission() throws Exception {
 PackageInfo packageInfo = new PackageInfo();
 packageInfo.packageName = TEST_PACKAGE_NAME;
 packageInfo.requestedPermissions =
   new String[] {"android.permission.SEND_SMS", "android.permission.READ_SMS"};
 packageInfo.requestedPermissionsFlags = new int[] {0, 0}; // Not granted by default
 shadowPackageManager.installPackage(packageInfo);
 try {
  packageManager.grantRuntimePermission(
    // This permission is not granted to the package.
    TEST_PACKAGE_NAME, "android.permission.RECEIVE_SMS", Process.myUserHandle());
  fail("Exception expected");
 } catch (SecurityException expected) {
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = JELLY_BEAN_MR2)
public void getUserRestrictions() {
 assertThat(userManager.getUserRestrictions().size()).isEqualTo(0);
 UserHandle userHandle = Process.myUserHandle();
 shadowOf(userManager).setUserRestriction(userHandle, UserManager.ENSURE_VERIFY_APPS, true);
 Bundle restrictions = userManager.getUserRestrictions();
 assertThat(restrictions.size()).isEqualTo(1);
 assertThat(restrictions.getBoolean(UserManager.ENSURE_VERIFY_APPS)).isTrue();
 // make sure that the bundle is not an internal state
 restrictions.putBoolean("something", true);
 restrictions = userManager.getUserRestrictions();
 assertThat(restrictions.size()).isEqualTo(1);
 shadowOf(userManager).setUserRestriction(newUserHandle(10), UserManager.DISALLOW_CAMERA, true);
 assertThat(userManager.hasUserRestriction(UserManager.DISALLOW_CAMERA)).isFalse();
}

相关文章