android.app.Activity.getPackageManager()方法的使用及代码示例

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

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

Activity.getPackageManager介绍

暂无

代码示例

代码示例来源:origin: jiangqqlmj/FastDev4Android

public static String getPackageName(Activity activity) {
  String packageName = null;
  try {
    packageName = String.valueOf(activity.getPackageManager()
        .getPackageInfo(activity.getPackageName(), 0).packageName);
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }
  return packageName;
}

代码示例来源:origin: joyoyao/superCleanMaster

public static boolean isIntentSafe(Activity activity, Uri uri) {
    Intent mapCall = new Intent(Intent.ACTION_VIEW, uri);
    PackageManager packageManager = activity.getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(mapCall, 0);
    return activities.size() > 0;
  }
}

代码示例来源:origin: jiangqqlmj/FastDev4Android

public static String getVersionName(Activity activity) {
  String version = null;
  try {
    version = String.valueOf(activity.getPackageManager()
        .getPackageInfo(activity.getPackageName(), 0).versionName);
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }
  return version;
}

代码示例来源:origin: commonsguy/cw-omnibus

private String findTargetAppPackage(Intent intent) {
 PackageManager pm = activity.getPackageManager();
 List<ResolveInfo> availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
 if (availableApps != null) {
  for (String targetApp : targetApplications) {
   if (contains(availableApps, targetApp)) {
    return targetApp;
   }
  }
 }
 return null;
}

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

private PackageInfo getPackageInfo(){
  
  if(pi == null){
    try {
      pi = act.getPackageManager().getPackageInfo(getAppId(), 0);
      
    } catch (NameNotFoundException e) {
      e.printStackTrace();
    }
  }
  return pi;
}

代码示例来源:origin: facebook/facebook-android-sdk

private static void shareToMessenger20150314(
  Activity activity,
  int requestCode,
  ShareToMessengerParams shareToMessengerParams) {
 try {
  Intent shareIntent = new Intent(Intent.ACTION_SEND);
  shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  shareIntent.setPackage(PACKAGE_NAME);
  shareIntent.putExtra(Intent.EXTRA_STREAM, shareToMessengerParams.uri);
  shareIntent.setType(shareToMessengerParams.mimeType);
  String appId = FacebookSdk.getApplicationId();
  if (appId != null) {
   shareIntent.putExtra(EXTRA_PROTOCOL_VERSION, PROTOCOL_VERSION_20150314);
   shareIntent.putExtra(EXTRA_APP_ID, appId);
   shareIntent.putExtra(EXTRA_METADATA, shareToMessengerParams.metaData);
   shareIntent.putExtra(EXTRA_EXTERNAL_URI, shareToMessengerParams.externalUri);
  }
  activity.startActivityForResult(shareIntent, requestCode);
 } catch (ActivityNotFoundException e) {
  Intent openMessenger = activity.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
  activity.startActivity(openMessenger);
 }
}

代码示例来源:origin: stackoverflow.com

private static void killProcessesAround(Activity activity) throws NameNotFoundException {
  ActivityManager am = (ActivityManager)activity.getSystemService(Context.ACTIVITY_SERVICE);
  String myProcessPrefix = activity.getApplicationInfo().processName;
  String myProcessName = activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0).processName;
  for (ActivityManager.RunningAppProcessInfo proc : am.getRunningAppProcesses()) {
    if (proc.processName.startsWith(myProcessPrefix) && !proc.processName.equals(myProcessName)) {
      android.os.Process.killProcess(proc.pid);
    }
  }
}

代码示例来源:origin: Justson/AgentWeb

private int queryActiviesNumber(String url) {
  try {
    if (mWeakReference.get() == null) {
      return 0;
    }
    Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
    PackageManager mPackageManager = mWeakReference.get().getPackageManager();
    List<ResolveInfo> mResolveInfos = mPackageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return mResolveInfos == null ? 0 : mResolveInfos.size();
  } catch (URISyntaxException ignore) {
    if (LogUtils.isDebug()) {
      ignore.printStackTrace();
    }
    return 0;
  }
}

代码示例来源:origin: naman14/Timber

public static boolean hasEffectsPanel(final Activity activity) {
  final PackageManager packageManager = activity.getPackageManager();
  return packageManager.resolveActivity(createEffectsIntent(),
      PackageManager.MATCH_DEFAULT_ONLY) != null;
}

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

private Drawable getAppIcon(){
  Drawable d = getApplicationInfo().loadIcon(act.getPackageManager());
  return d;
}

代码示例来源:origin: crazycodeboy/TakePhoto

/**
 * 拍照前检查是否有相机
 **/
public static void captureBySafely(TContextWrap contextWrap, TIntentWap intentWap) throws TException {
  List result = contextWrap.getActivity().getPackageManager().queryIntentActivities(intentWap.getIntent(), PackageManager.MATCH_ALL);
  if (result.isEmpty()) {
    Toast.makeText(contextWrap.getActivity(), contextWrap.getActivity().getResources().getText(R.string.tip_no_camera),
      Toast.LENGTH_SHORT).show();
    throw new TException(TExceptionType.TYPE_NO_CAMERA);
  } else {
    startActivityForResult(contextWrap, intentWap);
  }
}

代码示例来源:origin: naman14/Timber

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void applyTaskDescription(@NonNull Activity activity, @Nullable String key, int color) {
  // Sets color of entry in the system recents page
  try {
    ActivityManager.TaskDescription td = new ActivityManager.TaskDescription(
        (String) activity.getTitle(),
        ((BitmapDrawable) activity.getApplicationInfo().loadIcon(activity.getPackageManager())).getBitmap(),
        color);
    activity.setTaskDescription(td);
  } catch (Exception ignored) {
  }
}

代码示例来源:origin: WVector/AppUpdate

public static boolean installApp(Activity activity, File appFile) {
  try {
    Intent intent = getInstallAppIntent(activity, appFile);
    if (activity.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
      activity.startActivityForResult(intent, REQ_CODE_INSTALL_APP);
    }
    return true;
  } catch (Exception e) {
    ExceptionHandler exceptionHandler = ExceptionHandlerHelper.getInstance();
    if (exceptionHandler != null) {
      exceptionHandler.onException(e);
    }
  }
  return false;
}

代码示例来源:origin: Justson/AgentWeb

private boolean lookup(String url) {
  try {
    Intent intent;
    Activity mActivity = null;
    if ((mActivity = mWeakReference.get()) == null) {
      return true;
    }
    PackageManager packageManager = mActivity.getPackageManager();
    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
    ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    // 跳到该应用
    if (info != null) {
      mActivity.startActivity(intent);
      return true;
    }
  } catch (Throwable ignore) {
    if (LogUtils.isDebug()) {
      ignore.printStackTrace();
    }
  }
  return false;
}

代码示例来源:origin: crazycodeboy/TakePhoto

/**
 * 安全地发送Intent
 *
 * @param contextWrap
 * @param intentWapList 要发送的Intent以及候选Intent
 * @param defaultIndex  默认发送的Intent
 * @param isCrop        是否为裁切照片的Intent
 * @throws TException
 */
public static void sendIntentBySafely(TContextWrap contextWrap, List<TIntentWap> intentWapList, int defaultIndex, boolean isCrop)
  throws TException {
  if (defaultIndex + 1 > intentWapList.size()) {
    throw new TException(isCrop ? TExceptionType.TYPE_NO_MATCH_PICK_INTENT : TExceptionType.TYPE_NO_MATCH_CROP_INTENT);
  }
  TIntentWap intentWap = intentWapList.get(defaultIndex);
  List result = contextWrap.getActivity().getPackageManager().queryIntentActivities(intentWap.getIntent(), PackageManager.MATCH_ALL);
  if (result.isEmpty()) {
    sendIntentBySafely(contextWrap, intentWapList, ++defaultIndex, isCrop);
  } else {
    startActivityForResult(contextWrap, intentWap);
  }
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 addPreferencesFromResource(R.xml.settings);
 pm=getActivity().getPackageManager();
 enabled=(SwitchPreference)findPreference(MonitorApp.PREF_ENABLED);
 populateWhitelist((MultiSelectListPreference)findPreference("whitelist"));
}

代码示例来源:origin: crazycodeboy/TakePhoto

/**
 * 通过第三方工具裁切照片,当没有第三方裁切工具时,会自动使用自带裁切工具进行裁切
 *
 * @param contextWrap
 * @param imageUri
 * @param outPutUri
 * @param options
 */
public static void cropWithOtherAppBySafely(TContextWrap contextWrap, Uri imageUri, Uri outPutUri, CropOptions options) {
  Intent nativeCropIntent = IntentUtils.getCropIntentWithOtherApp(imageUri, outPutUri, options);
  List result = contextWrap.getActivity().getPackageManager().queryIntentActivities(nativeCropIntent, PackageManager.MATCH_ALL);
  if (result.isEmpty()) {
    cropWithOwnApp(contextWrap, imageUri, outPutUri, options);
  } else {
    //            try {
    //                imageUri=Uri.fromFile(new File(TUriParse.getFilePathWithDocumentsUri(imageUri,contextWrap.getActivity())));
    //            } catch (TException e) {
    //                e.printStackTrace();
    //            }
    startActivityForResult(contextWrap,
      new TIntentWap(IntentUtils.getCropIntentWithOtherApp(imageUri, outPutUri, options), TConstant.RC_CROP));
  }
}

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

/** MCC + MNC are always present in config changes since Oreo. */
@Test
@Config(minSdk = O)
public void getActivityMetaData_configChangesAlwaysIncludesMccAndMnc() throws Exception {
 Activity activity = setupActivity(ShadowPackageManagerTest.ActivityWithConfigChanges.class);
 ActivityInfo activityInfo =
   activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0);
 int configChanges = activityInfo.configChanges;
 assertThat(configChanges & ActivityInfo.CONFIG_MCC).isEqualTo(ActivityInfo.CONFIG_MCC);
 assertThat(configChanges & ActivityInfo.CONFIG_MNC).isEqualTo(ActivityInfo.CONFIG_MNC);
}

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

@Test
public void getActivityMetaData_configChanges() throws Exception {
 Activity activity = setupActivity(ShadowPackageManagerTest.ActivityWithConfigChanges.class);
 ActivityInfo activityInfo =
   activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0);
 int configChanges = activityInfo.configChanges;
 assertThat(configChanges & ActivityInfo.CONFIG_SCREEN_LAYOUT)
   .isEqualTo(ActivityInfo.CONFIG_SCREEN_LAYOUT);
 assertThat(configChanges & ActivityInfo.CONFIG_ORIENTATION)
   .isEqualTo(ActivityInfo.CONFIG_ORIENTATION);
 // Spot check a few other possible values that shouldn't be in the flags.
 assertThat(configChanges & ActivityInfo.CONFIG_FONT_SCALE).isEqualTo(0);
 assertThat(configChanges & ActivityInfo.CONFIG_SCREEN_SIZE).isEqualTo(0);
}

代码示例来源:origin: facebook/facebook-android-sdk

@Before
public void setup() {
 mockStatic(Utility.class);
 mMockActivity = mock(Activity.class);
 mMockPackageManager = mock(PackageManager.class);
 when(mMockActivity.getPackageManager()).thenReturn(mMockPackageManager);
}

相关文章

Activity类方法