How to get the package name and class name of Apk in Android

 In the process of Android development, sometimes when we need to call a third-party APK application, we do not know the package name and class name. We can call the following method to obtain the required package name and class name.

  /** 
 * 
 * Get the package name and version of this APP through the APK address 
 * */

private void getPackageName() {
        String FilePath="*.apk";//Enter the APK address
        PackageManager pm = getPackageManager();    
        PackageInfo info = pm.getPackageArchiveInfo(FilePath, PackageManager.GET_ACTIVITIES);    
        if(info != null){    
            ApplicationInfo appInfo = info.applicationInfo;    
            String appName = pm.getApplicationLabel(appInfo).toString();    
            String packageName = appInfo.packageName; //Get Installation package name 
            Log.i("Abel_Test", "The package name is: "+packageName);
            String version=info.versionName; //Get version information 
Log.i("Abel_Tes", "Version information: "+version);

   }
        }

 

 /** 
 * 
 * Get the details of this APP by package name, including Activities, services, versioncode, name, etc. and can directly call
 * */

private void doStartApplicationWithPackageName(String packagename) {


PackageInfo packageinfo = null;
try {
packageinfo = getPackageManager( ).getPackageInfo(packagename, 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
return;
}


// Create an Intent with the package name of category CATEGORY_LAUNCHER
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageinfo.packageName);


// traverse through the queryIntentActivities method of getPackageManager()
List<ResolveInfo> resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);


ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
// packagename = parameter packname
String packageName = resolveinfo.activityInfo .packageName;
// This is the Activity of the LAUNCHER of the APP we are looking for [Organization: packagename.mainActivityname]
String className = resolveinfo.activityInfo.name;
Log.i("Abel_Test", "The class name is: "+className );
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);


// Set ComponentName parameter 1: packagename parameter 2: MainActivity path
ComponentName cn = new ComponentName(packageName, className);
       
intent.setComponent(cn);
startActivity(intent);
}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676883&siteId=291194637