android.app.Application.getPackageName()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(238)

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

Application.getPackageName介绍

暂无

代码示例

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

/**
 * Returns whether a permission should be treated as granted to the package for backward
 * compatibility reasons.
 *
 * Before Robolectric 4.0 the ShadowPackageManager treated every requested permission as
 * automatically granted. 4.0 changes this behavior, and only treats a permission as granted if
 * PackageInfo.requestedPermissionFlags[permissionIndex] & REQUESTED_PERMISSION_GRANTED ==
 * REQUESTED_PERMISSION_GRANTED which matches the real PackageManager's behavior.
 *
 * Since many existing tests didn't set the requestedPermissionFlags on their {@code
 * PackageInfo} objects, but assumed that all permissions are granted, we auto-grant all
 * permissions if the requestedPermissionFlags is not set. If the requestedPermissionFlags is set,
 * we assume that the test is configuring the permission grant state, and we don't override this
 * setting.
 */
private boolean isGrantedForBackwardsCompatibility(String pkgName, PackageInfo permissionsInfo) {
 // Note: it might be cleaner to auto-grant these permissions when the package is added to the
 // PackageManager. But many existing tests modify the requested permissions _after_ adding the
 // package to the PackageManager, without updating the requestedPermissionsFlags.
 return permissionsInfo.requestedPermissionsFlags == null
   // Robolectric uses the PackageParser to create the current test package's PackageInfo from
   // the manifest XML. The parser populates the requestedPermissionsFlags, but doesn't grant
   // the permissions. Several tests rely on the test package being granted all permissions, so
   // we treat this as a special case.
   || pkgName.equals(RuntimeEnvironment.application.getPackageName());
}

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

public ShadowActivityManager() {
 ActivityManager.RunningAppProcessInfo processInfo = new ActivityManager.RunningAppProcessInfo();
 fillInProcessInfo(processInfo);
 processInfo.processName = RuntimeEnvironment.application.getPackageName();
 processInfo.pkgList = new String[] {RuntimeEnvironment.application.getPackageName()};
 processes.add(processInfo);
}

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

@Implementation(minSdk = JELLY_BEAN_MR1)
protected String getCreatorPackage() {
 return (creatorPackage == null)
   ? RuntimeEnvironment.application.getPackageName()
   : creatorPackage;
}

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

private ActivityInfo getActivityInfo(Application application) {
 try {
  return application.getPackageManager().getActivityInfo(new ComponentName(application.getPackageName(), component.getClass().getName()), PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA);
 } catch (PackageManager.NameNotFoundException e) {
  throw new RuntimeException(e);
 }
}

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

private ResName qualifyFromNonAssetFileName(String fileName) {
 // Resources from a jar belong to the "android" namespace, except when they come from "resource_files.zip"
 // when they are application resources produced by Bazel.
 if (fileName.startsWith("jar:") && !fileName.contains("resource_files.zip")) {
  // Must remove "jar:" prefix, or else qualifyFromFilePath fails on Windows
  return ResName.qualifyFromFilePath("android", fileName.replaceFirst("jar:", ""));
 } else {
  return ResName.qualifyFromFilePath(RuntimeEnvironment.application.getPackageName(), fileName);
 }
}

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

/** Removes a USB device from available USB devices map. */
public void removeUsbDevice(UsbDevice usbDevice) {
 Preconditions.checkNotNull(usbDevice);
 usbDevices.remove(usbDevice.getDeviceName());
 revokePermission(usbDevice, RuntimeEnvironment.application.getPackageName());
}

代码示例来源:origin: square/leakcanary

@Override public void run() {
  //noinspection ResultOfMethodCallIgnored
  heapDumpFile.setReadable(true, false);
  final Uri heapDumpUri = getUriForFile(getBaseContext(),
    "com.squareup.leakcanary.fileprovider." + getApplication().getPackageName(),
    heapDumpFile);
  runOnUiThread(new Runnable() {
   @Override public void run() {
    startShareIntentChooser(heapDumpUri);
   }
  });
 }
});

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

@Override public String getPackageName() {
  return sEnablePackageNameFake ? FAKE_PACKAGE_NAME : super.getPackageName();
}

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

private static void doValidateProcessNames(final Application app, final String[] process_names) {
  try {
    final PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(),
        GET_ACTIVITIES | GET_SERVICES | GET_RECEIVERS | GET_PROVIDERS);
    final Set<String> defined_process_names = new HashSet<>();
    if (info.activities != null) for (final ActivityInfo activity : info.activities) defined_process_names.add(activity.processName);
    if (info.services != null) for (final ServiceInfo service : info.services) defined_process_names.add(service.processName);
    if (info.receivers != null) for (final ActivityInfo receiver : info.receivers) defined_process_names.add(receiver.processName);
    if (info.providers != null) for (final ProviderInfo provider : info.providers) defined_process_names.add(provider.processName);
    for (final String process_name : process_names)
      if (! defined_process_names.contains(getFullProcessName(app, process_name)))
        throw new IllegalArgumentException("Process name \"" + process_name + "\" is not used by any component in AndroidManifest.xml");
  } catch (final PackageManager.NameNotFoundException ignored) {}		// Should never happen
}

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

/** Returns true if the caller has permission to access the device. */
@Implementation
protected boolean hasPermission(UsbDevice device) {
 return hasPermissionForPackage(device, RuntimeEnvironment.application.getPackageName());
}

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

/**
 * Adds a USB device into available USB devices map with permission value. If the USB device
 * already exists, updates the USB device with new permission value.
 */
public void addOrUpdateUsbDevice(UsbDevice usbDevice, boolean hasPermission) {
 Preconditions.checkNotNull(usbDevice);
 Preconditions.checkNotNull(usbDevice.getDeviceName());
 usbDevices.put(usbDevice.getDeviceName(), usbDevice);
 if (hasPermission) {
  grantPermission(usbDevice);
 } else {
  revokePermission(usbDevice, RuntimeEnvironment.application.getPackageName());
 }
}

代码示例来源: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 = N)
@HiddenApi
protected void grantPermission(UsbDevice device) {
 grantPermission(device, RuntimeEnvironment.application.getPackageName());
}

代码示例来源:origin: ACRA/acra

/**
 * {@inheritDoc}
 */
@Override
public void setEnabled(boolean enabled) {
  if (supportedAndroidVersion) {
    ACRA.log.i(LOG_TAG, "ACRA is " + (enabled ? "enabled" : "disabled") + " for " + context.getPackageName());
    reportExecutor.setEnabled(enabled);
  } else {
    ACRA.log.w(LOG_TAG, "ACRA requires ICS or greater. ACRA is disabled and will NOT catch crashes or send messages.");
  }
}

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

@Test
public void createPackageContextRemoteViews() throws Exception {
 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remote_views);
 remoteViews.apply(context, new FrameLayout(context));
}

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

@Test
public void createPackageContext() throws Exception {
 Context packageContext = context.createPackageContext(context.getPackageName(), 0);
 LayoutInflater inflater =
   (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 inflater.cloneInContext(packageContext);
 inflater.inflate(R.layout.remote_views, new FrameLayout(context), false);
}

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

@Test public void shouldAssignThePackageNameFromTheManifest() throws Exception {
 Application application = ApplicationProvider.getApplicationContext();
 assertThat(application.getPackageName()).isEqualTo("org.robolectric");
 assertThat(application).isInstanceOf(TestApplication.class);
}

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

@Test
public void create_withResourceId_shouldSetDataSource() {
 Application context = ApplicationProvider.getApplicationContext();
 ShadowMediaPlayer.addMediaInfo(
   DataSource.toDataSource("android.resource://" + context.getPackageName() + "/123"),
   new ShadowMediaPlayer.MediaInfo(100, 10));
 MediaPlayer mp = MediaPlayer.create(context, 123);
 ShadowMediaPlayer shadow = shadowOf(mp);
 assertThat(shadow.getDataSource())
   .isEqualTo(
     DataSource.toDataSource("android.resource://" + context.getPackageName() + "/123"));
}

代码示例来源:origin: ACRA/acra

@Before
public void setUp() throws Exception {
  ACRA.log = new RobolectricLog();
  ACRA.DEV_LOGGING = true;
  Robolectric.setupContentProvider(AcraContentProvider.class, RuntimeEnvironment.application.getPackageName() + ".acra");
  resolver = RuntimeEnvironment.application.getContentResolver();
  file = File.createTempFile("test", "." + JSON_EXTENSION);
  Shadows.shadowOf(MimeTypeMap.getSingleton()).addExtensionMimeTypMapping(JSON_EXTENSION, JSON_MIMETYPE);
}

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

@Test
@Config(minSdk = LOLLIPOP)
public void enforcePermissionChecks() throws Exception {
 shadowOf(userManager).enforcePermissionChecks(true);
 try {
  userManager.isManagedProfile();
  fail("Expected exception");
 } catch (SecurityException expected) {}
 Application context = ApplicationProvider.getApplicationContext();
 PackageInfo packageInfo =
   shadowOf(context.getPackageManager())
     .getInternalMutablePackageInfo(context.getPackageName());
 packageInfo.requestedPermissions = new String[] {permission.MANAGE_USERS};
 shadowOf(userManager).setManagedProfile(true);
 assertThat(userManager.isManagedProfile()).isTrue();
}

相关文章

微信公众号

最新文章

更多

Application类方法