android 获取当前运行的APP及Activity
aosp/frameworks/base/services/core/java/com/android/server/wm/DisplayPolicy.java 在此方法位置可以打印出当前系统显示的是哪一APP,运行在最前面,callingPid及为当前的APP的进程ID号,
public void adjustWindowParamsLw(WindowState win, WindowManager.LayoutParams attrs, int callingPid, int callingUid)
{
}
获取当前APP包名及Activity类名
private void getTopAppNameAndTopActivityName() { android.app.ActivityManager am = (android.app.ActivityManager) mContext.getSystemService(android.content.Context.ACTIVITY_SERVICE); java.util.List<android.app.ActivityManager.RunningTaskInfo> runningTasks = am.getRunningTasks(1); if (runningTasks != null && !runningTasks.isEmpty()) { android.app.ActivityManager.RunningTaskInfo taskInfo = runningTasks.get(0); String toApppkg = taskInfo.topActivity.getPackageName()); String toActivity = taskInfo.topActivity.getClassName()); } } taskInfo.baseActivity.getPackageName(); 获取应用包名,与taskInfo.topActivity.getPackageName()一样的返回值, taskInfo.baseActivity.getClassName(); 此处返回当前运行应用的AndroidManifest.xml声明为action android:name="android.intent.action.MAIN"的activity类,它是固定不变的,
//获取当前界面activity类名
private String getTopActivityClassName() { String topActivityClass = null; ActivityManager activityManager = (ActivityManager) (mContext .getSystemService(Context.ACTIVITY_SERVICE)); try { List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1); if (runningTaskInfos != null && runningTaskInfos.size() > 0) { ComponentName f = runningTaskInfos.get(0).topActivity; topActivityClass = f.getClassName(); } } catch (Exception e) { } return topActivityClass; }
//判断应用是否为系统应用
public static boolean isSystemApp(Context context, String packageName) { boolean result = false; if (packageName != null) { PackageManager pm = context.getPackageManager(); if (packageName != null) { try { ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0); if (appInfo != null) { if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) { result = true; } } } catch (PackageManager.NameNotFoundException e) { Log.e(TAG_LOG, TAG + " Error getting application info:", e); } } } return result; }
//通过包名获取UID 与 进程名和包名
PackageInfo info = mContext.getPackageManager().getPackageInfo(pkg, 0); //android.util.Log.i("xxx", " info.applicationInfo.processName = " + info.applicationInfo.processName); //android.util.Log.i("xxx", " info.applicationInfo.uid = " + info.applicationInfo.uid); //通过UID获取包名 try { PackageManager pm = mContext.getPackageManager(); ApplicationInfo ai = pm.getApplicationInfo(pkg, PackageManager.GET_ACTIVITIES); String[] pkgNames = mContext.getPackageManager().getPackagesForUid(ai.uid); if (pkgNames != null) { for (int j = 0; j < pkgNames.length; j++) { //android.util.Log.i("xxxx","pkgname = "+ pkgNames[j]); } } } catch (Exception e) { e.printStackTrace(); }
版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论