android.content.Context.getPackageName()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(1168)

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

Context.getPackageName介绍

暂无

代码示例

代码示例来源:origin: bumptech/glide

@Nullable
private static PackageInfo getPackageInfo(@NonNull Context context) {
 try {
  return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
 } catch (PackageManager.NameNotFoundException e) {
  Log.e(TAG, "Cannot resolve info for" + context.getPackageName(), e);
  return null;
 }
}

代码示例来源:origin: google/ExoPlayer

/**
 * Returns a user agent string based on the given application name and the library version.
 *
 * @param context A valid context of the calling application.
 * @param applicationName String that will be prefix'ed to the generated user agent.
 * @return A user agent string generated using the applicationName and the library version.
 */
public static String getUserAgent(Context context, String applicationName) {
 String versionName;
 try {
  String packageName = context.getPackageName();
  PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
  versionName = info.versionName;
 } catch (NameNotFoundException e) {
  versionName = "?";
 }
 return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE
   + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY;
}

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

/**
   * Returns true if the application is debuggable.
   *
   * @return true if the application is debuggable.
   */
  private boolean isDebuggable() {
    final PackageManager pm = context.getPackageManager();
    try {
      return (pm.getApplicationInfo(context.getPackageName(), 0).flags & ApplicationInfo.FLAG_DEBUGGABLE) > 0;
    } catch (PackageManager.NameNotFoundException e) {
      return false;
    }
  }
}

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

Context context = getApplicationContext(); // or activity.getApplicationContext()
PackageManager packageManager = context.getPackageManager();
String packageName = context.getPackageName();

String myVersionName = "not available"; // initialize String

try {
  myVersionName = packageManager.getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
  e.printStackTrace();
}

代码示例来源:origin: LawnchairLauncher/Lawnchair

public static int getBuildNumber(Context context) {
  try {
    return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
  } catch (PackageManager.NameNotFoundException ex) {}
  return 0;
}

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 获取版本号
 *
 * @param context
 * @return
 */
public static int getVersionCode(Context context) {
  int versionCode = 0;
  try {
    versionCode = context.getPackageManager()
        .getPackageInfo(context.getPackageName(),
            0).versionCode;
  } catch (PackageManager.NameNotFoundException ex) {
    versionCode = 0;
  }
  return versionCode;
}

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 获取版本名
 *
 * @param context
 * @return
 */
public static String getVersionName(Context context) {
  String name = "";
  try {
    name = context.getPackageManager()
        .getPackageInfo(context.getPackageName(),
            0).versionName;
  } catch (PackageManager.NameNotFoundException ex) {
    name = "";
  }
  return name;
}

代码示例来源:origin: mttkay/ignition

public static String getApplicationVersionString(Context context) {
  try {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
    return "v" + info.versionName;
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}

代码示例来源:origin: GitLqr/LQRWeChat

/**
 * 重启当前应用
 */
public static void restart() {
  Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName());
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  mContext.startActivity(intent);
}

代码示例来源:origin: Tencent/tinker

public static String getManifestTinkerID(Context context) {
  if (tinkerID != null) {
    return tinkerID;
  }
  try {
    ApplicationInfo appInfo = context.getPackageManager()
      .getApplicationInfo(context.getPackageName(),
        PackageManager.GET_META_DATA);
    Object object = appInfo.metaData.get(ShareConstants.TINKER_ID);
    if (object != null) {
      tinkerID = String.valueOf(object);
    } else {
      tinkerID = null;
    }
  } catch (Exception e) {
    Log.e(TAG, "getManifestTinkerID exception:" + e.getMessage());
    return null;
  }
  return tinkerID;
}

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

public static String getManifestString(Context context, String name) {
  try {
    ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    return appInfo.metaData.getString(name);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: facebook/stetho

private String getAppLabelAndVersion() {
 StringBuilder b = new StringBuilder();
 PackageManager pm = mContext.getPackageManager();
 b.append(getAppLabel());
 b.append('/');
 try {
  PackageInfo info = pm.getPackageInfo(mContext.getPackageName(), 0 /* flags */);
  b.append(info.versionName);
 } catch (PackageManager.NameNotFoundException e) {
  throw new RuntimeException(e);
 }
 return b.toString();
}

代码示例来源:origin: sunfusheng/MarqueeView

public static String getVersionName(Context context) {
  try {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
    String version = packInfo.versionName;
    if (!TextUtils.isEmpty(version)) {
      return version;
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return "";
}

代码示例来源:origin: sunfusheng/StickyHeaderListView

public static String getVersionName(Context context) {
  try {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
    String version = packInfo.versionName;
    if (!TextUtils.isEmpty(version)) {
      return version;
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return "";
}

代码示例来源:origin: android-hacker/VirtualXposed

public void onCreate(Context context) {
  AttributeCache.init(context);
  PackageManager pm = context.getPackageManager();
  PackageInfo packageInfo = null;
  try {
    packageInfo = pm.getPackageInfo(context.getPackageName(),
        PackageManager.GET_ACTIVITIES | PackageManager.GET_PROVIDERS | PackageManager.GET_META_DATA);
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }
  if (packageInfo == null) {
    throw new RuntimeException("Unable to found PackageInfo : " + context.getPackageName());
  }
  sService.set(this);
}

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

private boolean hasManageUsersPermission() {
 return context.getPackageManager().checkPermission(permission.MANAGE_USERS, context.getPackageName()) == PackageManager.PERMISSION_GRANTED;
}

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

public static String getAppVersion() {
    Context context = FacebookSdk.getApplicationContext();
    try {
      PackageInfo pInfo = context.getPackageManager()
          .getPackageInfo(context.getPackageName(), 0);
      return pInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
      return "";
    }
  }
}

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

@Nullable
private static String getInstallerPackageName(Context context) {
  PackageManager packageManager = context.getPackageManager();
  if (packageManager != null) {
    return packageManager.getInstallerPackageName(context.getPackageName());
  }
  return null;
}

代码示例来源:origin: wdullaer/MaterialDateTimePicker

/**
 * Method to verify that vibrate permission has been granted.
 *
 * Allows users of the library to disabled vibrate support if desired.
 * @return true if Vibrate permission has been granted
 */
private boolean hasVibratePermission(Context context) {
  PackageManager pm = context.getPackageManager();
  int hasPerm = pm.checkPermission(android.Manifest.permission.VIBRATE, context.getPackageName());
  return hasPerm == PackageManager.PERMISSION_GRANTED;
}

代码示例来源:origin: TommyLemon/APIJSON

/**检查是否有权限
 * @param context
 * @param name
 * @return
 */
public static boolean isHavePermission(Context context, String name){
  try {
    return PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(name, context.getPackageName());
  } catch (Exception e) {
    // TODO: handle exception
  }
  return false;
}

相关文章

Context类方法