简体   繁体   中英

Programmatically check if an Activity is declared in the AndroidManifest.xml

I want to check if an activity is declared in the app's manifest.

Some ads platform, like MMedia, Admob, etc need concret activities to be declared in your manifest. I want to automate a test that checks if those activities has been declared. Some of this libraries throws an exception if you forget to declare the activities, other just do not work.

For example, checking if the activity com.google.ads.AdActivity is in my manifest

Thanks

The PackageManager contains all information of declared services, activities etc.

Use getInstalledPackages() with the GET_ACTIVITIES flag.

If it's not in there, it's not declared.

Example:

List<PackageInfo> pInfos = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
for (PackageInfo pInfo : pInfos) {
  ActivityInfo[] aInfos = pInfo.activities;
  if (aInfos != null) {
    for (ActivityInfo activityInfo : aInfos) {
      Log.i("ACT", activityInfo.name);
      // do whatever else you like... 
    }
  }
}

You can also use packageinfo:

PackageManager pm = getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);

packageInfo.activities // contains a list of all your activity tags under <application>,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM