方法一:PackageManager獲取:android
PackageManager packageManager = getPackageManager(); PackageInfo packageInfo = null; try { packageInfo = packageManager.getPackageInfo( getPackageName(), PackageManager.GET_ACTIVITIES); for (ActivityInfo activity : packageInfo.activities) { Class<?> aClass = Class.forName(activity.name); Log.i("packageManager ","===========activityList======"+aClass); } } catch (Exception e) { e.printStackTrace(); }
方法二:只能查詢指定actionapp
Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.setPackage(context.getPackageName()); for (ResolveInfo info : context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_ALL)) { Log.w("new_hijack","[activtiy]"+info.activityInfo.name+","+info.activityInfo.processName); }
方法三:只能查詢已加載的code
List<Activity> list = new ArrayList<>(); try { Class<Application> applicationClass = Application.class; Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk"); mLoadedApkField.setAccessible(true); Object mLoadedApk = mLoadedApkField.get(application); Class<?> mLoadedApkClass = mLoadedApk.getClass(); Field mActivityThreadField = mLoadedApkClass.getDeclaredField("mActivityThread"); mActivityThreadField.setAccessible(true); Object mActivityThread = mActivityThreadField.get(mLoadedApk); Class<?> mActivityThreadClass = mActivityThread.getClass(); Field mActivitiesField = mActivityThreadClass.getDeclaredField("mActivities"); mActivitiesField.setAccessible(true); // ActivityThread.ActivityClientRecord Object mActivities = mActivitiesField.get(mActivityThread); // 注意這裏必定寫成Map,低版本這裏用的是HashMap,高版本用的是ArrayMap if (mActivities instanceof Map) { @SuppressWarnings("unchecked") Map<Object, Object> arrayMap = (Map<Object, Object>) mActivities; for (Map.Entry<Object, Object> entry : arrayMap.entrySet()) { Object value = entry.getValue(); Class<?> activityClientRecordClass = value.getClass(); Field activityField = activityClientRecordClass.getDeclaredField("activity"); activityField.setAccessible(true); Object o = activityField.get(value); Log.w("new_hijack","ACTIVITY"+o.getClass().getName()); list.add((Activity) o); } } } catch (Exception e) { e.printStackTrace(); list = null; }
方法四:反射獲取方法component
Class packageParserClass = Class.forName("android.content.pm.PackageParser"); Method parsePackageMethod = packageParserClass.getDeclaredMethod("parsePackage", File.class, int.class); Object packageParser = packageParserClass.newInstance(); Object packageObj= parsePackageMethod.invoke(packageParser, new File(application.getApplicationInfo().sourceDir), PackageManager.GET_ACTIVITIES); Field receiverField=packageObj.getClass().getDeclaredField("activities"); // Field receiverField=packageObj.getClass().getDeclaredField("receivers"); List receivers = (List) receiverField.get(packageObj); Class<?> componentClass = Class.forName("android.content.pm.PackageParser$Component"); Field intentsField = componentClass.getDeclaredField("intents"); // 調用generateActivityInfo 方法, 把PackageParser.Activity 轉換成 Class<?> packageParser$ActivityClass = Class.forName("android.content.pm.PackageParser$Activity"); // generateActivityInfo方法 Class<?> packageUserStateClass = Class.forName("android.content.pm.PackageUserState"); Object defaltUserState= packageUserStateClass.newInstance(); Method generateReceiverInfo = packageParserClass.getDeclaredMethod("generateActivityInfo", packageParser$ActivityClass, int.class, packageUserStateClass, int.class); Class<?> userHandler = Class.forName("android.os.UserHandle"); Method getCallingUserIdMethod = userHandler.getDeclaredMethod("getCallingUserId"); int userId = (int) getCallingUserIdMethod.invoke(null); for (Object activity : receivers) { ActivityInfo info= (ActivityInfo) generateReceiverInfo.invoke(packageParser, activity,0, defaltUserState, userId); Log.w("new_hijack","activtiy:"+info.name); }