/** * get information for process * * @return true == success, false == failed */ public boolean getCacheInfo() { if (queryQueue.isEmpty()) return false; try { queryQueueLock.acquire(); } catch (InterruptedException e) { return false; } QueueJob processJob = queryQueue.remove(); queryQueueLock.release(); PackageInfo curPackageInfo = null; String curPackageName = null; if (processJob.process.contains(":")) curPackageName = processJob.process.substring(0, processJob.process.indexOf(":")); else curPackageName = processJob.process; // for system user if (processJob.owner.contains("system") && processJob.process.contains("system") && !processJob.process.contains(".")) curPackageName = "android"; try { curPackageInfo = packageMgr.getPackageInfo(curPackageName, 0); } catch (NameNotFoundException e) { } if (curPackageInfo == null && processJob.uid > 0) { String[] subPackageName = packageMgr.getPackagesForUid(processJob.uid); if (subPackageName != null) { for (int PackagePtr = 0; PackagePtr < subPackageName.length; PackagePtr++) { if (subPackageName[PackagePtr] == null) continue; try { curPackageInfo = packageMgr.getPackageInfo(subPackageName[PackagePtr], 0); PackagePtr = subPackageName.length; } catch (NameNotFoundException e) { } } } } CacheItem processItem = new CacheItem(); if (curPackageInfo != null) { processItem.name = curPackageInfo.applicationInfo.loadLabel(packageMgr).toString(); if (useDetail == true) processItem.icon = resizeImage(curPackageInfo.applicationInfo.loadIcon(packageMgr), iconSize); } else { processItem.name = curPackageName; processItem.icon = commonIcon; } cacheStorage.put(processJob.process, processItem); return true; }
public void collectCrashDeviceInfo(Context ctx) { try { PackageManager pm = ctx.getPackageManager(); PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES); if (pi != null) { mDeviceCrashInfo.put(VERSION_NAME, pi.versionName == null ? "not set" : pi.versionName); mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode); } } catch (NameNotFoundException e) { if (Constant.DEBUG) { e.printStackTrace(); } // Log.e(TAG, "Error while collect package info", e); } Field[] fields = Build.class.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); mDeviceCrashInfo.put(field.getName(), field.get(null)); if (Constant.DEBUG) { System.out.println(field.getName() + " : " + field.get(null)); } } catch (Exception e) { if (Constant.DEBUG) { e.printStackTrace(); } // Log.e(TAG, "Error while collect crash info", e); } } }
/** Build {@link UidDetail} object, blocking until all {@link Drawable} lookup is finished. */ private UidDetail buildUidDetail(int uid) { final Resources res = mContext.getResources(); final PackageManager pm = mContext.getPackageManager(); final UidDetail detail = new UidDetail(); detail.label = pm.getNameForUid(uid); detail.icon = pm.getDefaultActivityIcon(); // handle special case labels switch (uid) { case android.os.Process.SYSTEM_UID: detail.label = res.getString(R.string.process_kernel_label); detail.icon = pm.getDefaultActivityIcon(); return detail; case TrafficStats.UID_REMOVED: detail.label = res.getString(R.string.data_usage_uninstalled_apps); detail.icon = pm.getDefaultActivityIcon(); return detail; case TrafficStats.UID_TETHERING: final ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); detail.label = res.getString(Utils.getTetheringLabel(cm)); detail.icon = pm.getDefaultActivityIcon(); return detail; } // otherwise fall back to using packagemanager labels final String[] packageNames = pm.getPackagesForUid(uid); final int length = packageNames != null ? packageNames.length : 0; try { if (length == 1) { final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0); detail.label = info.loadLabel(pm).toString(); detail.icon = info.loadIcon(pm); } else if (length > 1) { detail.detailLabels = new CharSequence[length]; for (int i = 0; i < length; i++) { final String packageName = packageNames[i]; final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0); final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0); detail.detailLabels[i] = appInfo.loadLabel(pm).toString(); if (packageInfo.sharedUserLabel != 0) { detail.label = pm.getText(packageName, packageInfo.sharedUserLabel, packageInfo.applicationInfo) .toString(); detail.icon = appInfo.loadIcon(pm); } } } } catch (NameNotFoundException e) { } if (TextUtils.isEmpty(detail.label)) { detail.label = Integer.toString(uid); } return detail; }
public static String getApplicationSignature(Context context) { if (context == null) { return null; } PackageManager packageManager = context.getPackageManager(); if (packageManager == null) { return null; } String packageName = context.getPackageName(); PackageInfo pInfo; try { pInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); } catch (PackageManager.NameNotFoundException e) { return null; } Signature[] signatures = pInfo.signatures; if (signatures == null || signatures.length == 0) { return null; } MessageDigest md; try { md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { return null; } md.update(pInfo.signatures[0].toByteArray()); return Base64.encodeToString(md.digest(), Base64.URL_SAFE | Base64.NO_PADDING); }
/** @param ctx context */ public void collectCrashDeviceInfo(Context ctx) { try { // Class for retrieving various kinds of information related to the // application packages that are currently installed on the device. // You can find this class through getPackageManager(). PackageManager pm = ctx.getPackageManager(); // getPackageInfo(String packageName, int flags) // Retrieve overall information about an application package that is installed on the system. // public static final int GET_ACTIVITIES // Since: API Level 1 PackageInfo flag: return information about activities in the package in // activities. PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES); if (pi != null) { // public String versionName The version name of this package, // as specified by the <manifest> tag's versionName attribute. mDeviceCrashInfo.put(VERSION_NAME, pi.versionName == null ? "not set" : pi.versionName); // public int versionCode The version number of this package, // as specified by the <manifest> tag's versionCode attribute. mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode); } } catch (NameNotFoundException e) { Log.e(TAG, "Error while collect package info", e); } Field[] fields = Build.class.getDeclaredFields(); for (Field field : fields) { try { // setAccessible(boolean flag) field.setAccessible(true); mDeviceCrashInfo.put(field.getName(), field.get(null)); Log.d(TAG, field.getName() + " : " + field.get(null)); } catch (Exception e) { Log.e(TAG, "Error while collect crash info", e); } } }
/** * 收集程序崩溃的设备信息 * * @param ctx */ public void collectCrashDeviceInfo(Context ctx) { try { PackageManager pm = ctx.getPackageManager(); PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES); if (pi != null) { mDeviceCrashInfo.put(VERSION_NAME, pi.versionName == null ? "not set" : pi.versionName); mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode + ""); } } catch (NameNotFoundException e) { Log.e(TAG, "Error while collect package info", e); } // 使用反射来收集设备信息.在Build类中包含各种设备信息, // 例如: 系统版本号,设备生产商 等帮助调试程序的有用信息 // 返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段 Field[] fields = Build.class.getDeclaredFields(); for (Field field : fields) { try { // setAccessible(boolean flag) // 将此对象的 accessible 标志设置为指示的布尔值。 // 通过设置Accessible属性为true,才能对私有变量进行访问,不然会得到一个IllegalAccessException的异常 field.setAccessible(true); mDeviceCrashInfo.put(field.getName(), field.get(null) + ""); } catch (Exception e) { Log.e(TAG, "Error while collect crash info", e); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_about); mToolbar = (Toolbar) findViewById(R.id.toolbar); // 初始化Toolbar setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowTitleEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // 获取APP版本号(名) try { TextView versionView = (TextView) findViewById(R.id.about_version); PackageManager packageManager = getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0); versionView.setText("V" + packageInfo.versionName); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } mToolbar.setNavigationOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); }
public void init() { instance = this; session = new HashMap<String, Object>(); activityManager = new ArrayList<Activity>(); if (fileDir != null) { imageLoader = new ImageLoader( instance, defaultImageId, fileDir); // 使用ImageLoader组件时,设置defaultImageId、fileDir } else { imageLoader = new ImageLoader(instance, defaultImageId); // 使用ImageLoader组件时,设置defaultImageId } PackageManager manager = this.getPackageManager(); try { PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0); deviceid = getDeviceId(); osVersion = Build.VERSION.RELEASE; mobileType = Build.MODEL; if (null != info) { version = info.versionName; versionCode = info.versionCode; } } catch (NameNotFoundException e) { e.printStackTrace(); } }
private static List<String> getPackageDatas(Context context) { List<String> packageDatas = new ArrayList<String>(); PackageManager packageManager = context.getPackageManager(); List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo appInfo : list) { StringBuffer data = new StringBuffer(); try { PackageInfo packageInfo = packageManager.getPackageInfo(appInfo.packageName, PackageManager.GET_META_DATA); data.append(appInfo.packageName); // パッケージ名 data.append(","); data.append(packageInfo.versionName); // バージョン } catch (NameNotFoundException e) { e.printStackTrace(); } packageDatas.add(data.toString()); } Collections.sort( packageDatas, new Comparator<String>() { public int compare(String lhs, String rhs) { return lhs.compareTo(rhs); } }); return packageDatas; }
/** Must not hold the Context. */ public ApplicationInfo( PackageManager pm, ResolveInfo info, IconCache iconCache, HashMap<Object, CharSequence> labelCache) { final String packageName = info.activityInfo.applicationInfo.packageName; this.componentName = new ComponentName(packageName, info.activityInfo.name); this.container = ItemInfo.NO_ID; this.setActivity( componentName, Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); try { int appFlags = pm.getApplicationInfo(packageName, 0).flags; if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) { flags |= DOWNLOADED_FLAG; if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { flags |= UPDATED_SYSTEM_APP_FLAG; } } firstInstallTime = pm.getPackageInfo(packageName, 0).firstInstallTime; } catch (NameNotFoundException e) { Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName); } iconCache.getTitleAndIcon(this, info, labelCache); }
public static ActivityInfo[] getActivityList(Context context, String packageName) throws NameNotFoundException { PackageManager pm = context.getPackageManager(); PackageInfo info = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); ActivityInfo[] list = info.activities; return list; }
public static String f(Context context) { if (e != null) { return e; } try { PackageManager packageManager = context.getPackageManager(); ResolveInfo resolveActivity = packageManager.resolveActivity( new Intent( "android.intent.action.VIEW", Uri.parse("market://details?id=com.google.ads")), Menu.CATEGORY_CONTAINER); if (resolveActivity == null) { return null; } ActivityInfo activityInfo = resolveActivity.activityInfo; if (activityInfo == null) { return null; } PackageInfo packageInfo = packageManager.getPackageInfo(activityInfo.packageName, 0); if (packageInfo == null) { return null; } String str = packageInfo.versionCode + "." + activityInfo.packageName; e = str; return str; } catch (NameNotFoundException e) { return null; } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_student_guanyu); context = this; TextView titletext = (TextView) findViewById(R.id.title_text); titletext.setText("关于师课家教宝"); findViewById(R.id.gongneng_jieshao_btn).setOnClickListener(listener); findViewById(R.id.faq_btn).setOnClickListener(listener); findViewById(R.id.wenti_fankui_btn).setOnClickListener(listener); findViewById(R.id.left_btn).setOnClickListener(listener); PackageManager packageManager = getPackageManager(); // getPackageName()是你当前类的包名,0代表是获取版本信息 PackageInfo packInfo = null; try { packInfo = packageManager.getPackageInfo(getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } String localVersion = packInfo.versionName; TextView versiontext = (TextView) findViewById(R.id.ver_name); versiontext.setText("当前版本: V" + localVersion); Sofy_Info(); }
/** * @param paramContext context * @since I used to be a programmer like you, then I took an arrow in the kneeVer 1.0 */ public void getDeviceInfo(Context paramContext) { try { PackageManager mPackageManager = paramContext.getPackageManager(); PackageInfo mPackageInfo = mPackageManager.getPackageInfo( paramContext.getPackageName(), PackageManager.GET_ACTIVITIES); if (mPackageInfo != null) { String versionName = mPackageInfo.versionName == null ? "null" : mPackageInfo.versionName; String versionCode = mPackageInfo.versionCode + ""; mDeviceInfo.put("versionName", versionName); mDeviceInfo.put("versionCode", versionCode); } } catch (NameNotFoundException e) { e.printStackTrace(); } Field[] mFields = Build.class.getDeclaredFields(); for (Field field : mFields) { try { field.setAccessible(true); mDeviceInfo.put(field.getName(), field.get("").toString()); Log.d(TAG, field.getName() + ":" + field.get("")); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
@SuppressLint("NewApi") public DiskLruCache(Context context, String cacheName, long cacheSize) { mContext = context; mCacheName = cacheName; mCacheSize = cacheSize; int version; PackageManager pm = context.getPackageManager(); try { PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0); version = pi.versionCode; } catch (NameNotFoundException e) { version = 1; } AsyncTask<Integer, Object, Object> task = new AsyncTask<Integer, Object, Object>() { @Override protected Object doInBackground(Integer... params) { initDiskCache(params[0]); return null; } }; if (Build.VERSION.SDK_INT >= 11) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, version); } else { task.execute(version); } }
/** * 注册PUSH服务 * * @param schoolid * @param schoolkey * @throws RemoteException */ public static void Register(String schoolid, String schoolkey) throws RemoteException { String packageName = mContext.getPackageName(); PushRegItem regItem = new PushRegItem(); regItem.packageName = packageName; regItem.SchoolID = schoolid; regItem.SchoolKey = schoolkey; regItem.appver = ""; PackageManager packageManager = mContext.getPackageManager(); try { PackageInfo packInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0); regItem.appver = packInfo.versionName; } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } mDrPalmPushService.RegAppPush( regItem, new IDrPalmPushCallback.Stub() { @Override public void onError(String err) throws RemoteException { // callback.onError(err); } @Override public void onSuccess() throws RemoteException { // callback.onSuccess(); } }); }
static boolean isSystemApp(Context context, Intent intent) { PackageManager pm = context.getPackageManager(); ComponentName cn = intent.getComponent(); String packageName = null; if (cn == null) { ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if ((info != null) && (info.activityInfo != null)) { packageName = info.activityInfo.packageName; } } else { packageName = cn.getPackageName(); } if (packageName != null) { try { PackageInfo info = pm.getPackageInfo(packageName, 0); return (info != null) && (info.applicationInfo != null) && ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); } catch (NameNotFoundException e) { return false; } } else { return false; } }
@Override public void uncaughtException(Thread thread, Throwable ex) { if (this.defaultUncaughtExceptionHandler != null) { if (mContext != null) { PackageManager manager = mContext.getPackageManager(); PackageInfo info = null; try { info = manager.getPackageInfo(mContext.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { Log.e("error", e.toString()); } Writer writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); Log.e("info", printWriter.toString()); // at this point we can log into local files and send information to crashlytics and other // stuffs final AlarmServiceVo userDataCollectorServiceVo = new AlarmServiceVo( Constants.AlarmManager.AlarmManagerRequestCode.COLLECT_USER_DATA, Constants.AlarmManager.COLLECT_USER_DATA_DELAY_MILLISECONDS, Constants.AlarmManager.COLLECT_USER_DATA_PERIODIC_TIME_MILLISECONDS); new UserDataAlarmManager(mContext, PointwiseService.class) .setUpAlarmManager(userDataCollectorServiceVo); final AlarmServiceVo userDataPeekerServiceVo = new AlarmServiceVo( Constants.AlarmManager.AlarmManagerRequestCode.PEEK_USER_DATA, Constants.AlarmManager.PEEK_USER_DATA_DELAY_MILLISECONDS, Constants.AlarmManager.PEEK_USER_DATA_PERIODIC_TIME_MILLISECONDS); new UserDataAlarmManager(mContext, PointwiseSyncService.class) .setUpAlarmManager(userDataPeekerServiceVo); } } }
@Override protected List<Label> doInBackground(Void... params) { final List<Label> allLabels = mClient.getAllLabels(); if ((allLabels == null) || allLabels.isEmpty()) { return null; } final PackageManager pm = mContext.getPackageManager(); final List<Label> candidates = new ArrayList<>(allLabels); ListIterator<Label> i = candidates.listIterator(); // Iterate through the labels database, and prune labels that belong // to valid packages. while (i.hasNext()) { final Label l = i.next(); // Ensure the label has a matching installed package. final String packageName = l.getPackageName(); PackageInfo packageInfo; try { packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); } catch (NameNotFoundException e) { // If there's no installed package, leave the label in the // list for removal. LogUtils.log( CustomLabelManager.class, Log.VERBOSE, "Consistency check removing label for unknown package %s.", packageName); continue; } // Ensure the signature hash of the application matches // the hash of the package when the label was stored. final String expectedHash = l.getPackageSignature(); final String actualHash = computePackageSignatureHash(packageInfo); if (TextUtils.isEmpty(expectedHash) || TextUtils.isEmpty(actualHash) || !expectedHash.equals(actualHash)) { // If the expected or actual signature hashes aren't // valid, or they don't match, leave the label in the list // for removal. LogUtils.log( CustomLabelManager.class, Log.WARN, "Consistency check removing label due to signature mismatch " + "for package %s.", packageName); continue; } // If the label has passed all consistency checks, prune the // label from the list of potentials for removal. i.remove(); } return candidates; // now containing only labels for removal }
public long getFirstInstallTime() { try { PackageInfo info = mPm.getPackageInfo(mActivityInfo.packageName, 0); return info != null ? info.firstInstallTime : 0; } catch (NameNotFoundException e) { return 0; } }
protected boolean isPackageInstalled(String packageName) { try { PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); return pi != null; } catch (PackageManager.NameNotFoundException e) { return false; } }
public String getAppVersion() { PackageManager pm = context.getPackageManager(); try { return pm.getPackageInfo(context.getPackageName(), 0).versionName; } catch (NameNotFoundException e) { return ""; } }
/** Get the package info for the app */ public static PackageInfo getAppPackageInfo(Context ctx) { try { PackageManager pkgMgr = ctx.getPackageManager(); return pkgMgr.getPackageInfo(ctx.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { return null; } }
public PackageHelper() { pm = ApplicationBean.getApplication().getPackageManager(); try { info = pm.getPackageInfo(ApplicationBean.getApplication().getPackageName(), 0); } catch (NameNotFoundException e) { e.printStackTrace(); } }
public static int getVersionCode(Context aContext) { try { PackageManager packageManager = aContext.getPackageManager(); PackageInfo packInfo = packageManager.getPackageInfo(aContext.getPackageName(), 0); return packInfo.versionCode; } catch (NameNotFoundException e) { return -1; } }
private boolean isPackageInstalled(String packagename, Context context) { PackageManager pm = context.getPackageManager(); try { pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } }
/** * @param packageName package name of the app * @param context application context * @return * @brief method for checking app is installed on device or not */ public static boolean isAppInstalled(Context context, String packageName) { PackageManager pm = context.getPackageManager(); try { pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } }
public static boolean isPackageInstalled(Context context, String targetPackage) { PackageManager pm = context.getPackageManager(); try { pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA); } catch (NameNotFoundException e) { return false; } return true; }
public static boolean isSystemApp(Context context) throws Exception { PackageManager pm = context.getPackageManager(); android.content.pm.PackageInfo info = pm.getPackageInfo("com.beanstalk.beanstalkota", PackageManager.GET_ACTIVITIES); ApplicationInfo aInfo = info.applicationInfo; String path = aInfo.sourceDir.substring(0, aInfo.sourceDir.lastIndexOf("/")); boolean isSystemApp = path.contains("system/app"); return isSystemApp; }
/** * 这个奇怪的函数 * * @param context * @param flags * @return */ public List<PackageInfo> getInstalledPackages(Context context, int flags) { try { return pm.getInstalledPackages(flags); } catch (Exception ignored) { // we don't care why it didn't succeed. We'll do it using an alternative way instead } // use fallback: Process process; List<PackageInfo> result = new ArrayList<>(); BufferedReader bufferedReader = null; try { process = Runtime.getRuntime().exec("pm list packages"); bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { final String packageName = line.substring(line.indexOf(':') + 1); final PackageInfo packageInfo = pm.getPackageInfo(packageName, flags); result.add(packageInfo); } process.waitFor(); } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedReader != null) try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } } return result; }