我需要通过控制packageName检查是否安装了apk。(如powerclean app - > app manager - > apkFiles)
为此,我需要从apk文件中获取packageName。(不是标签)
我已经尝试过this
了public void getApkPackage(String pathToApk) {
Process mProcess = null;
try {
mProcess = Runtime.getRuntime().exec("aapt dump badging "+pathToApk);
} catch (IOException e) {
e.printStackTrace();
}
}
但它返回
ava.io.IOException: Error running exec(). Command: [aapt, dump, badging, /storage/emulated/0/asd.apk] Working Directory: null Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java)
......
Caused by: java.io.IOException: Permission denied
at java.lang.ProcessManager.exec(Native Method)
我已添加
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
这些权限。
注意:我不想安装或未安装apk名称,我已经得到了它们。我想得到apk packageName ..
//get app name from apk
public static String getAppLabel(PackageManager pm, String pathToApk ,Context context) {
PackageInfo packageInfo = pm.getPackageArchiveInfo(pathToApk, 0);
if (Build.VERSION.SDK_INT >= 8) {
// those two lines do the magic:
packageInfo.applicationInfo.sourceDir = pathToApk;
packageInfo.applicationInfo.publicSourceDir = pathToApk;
}
CharSequence label = pm.getApplicationLabel(packageInfo.applicationInfo);
return label != null ? label.toString() : null;
}
更多信息部分:
我想做同样的事情。
1 - 获取手机中已有的所有apk路径(系统文件夹除外)
2 - 在手机上获取所有已安装的软件包(已经完成)
从创建的apks路径获取PackageName(问题从这里开始)
4 - 检查已安装包中的每个已创建的apks包名称(简单部分)
5 - 将它们添加到2个arraylist(已安装的apks - 未安装的apks)
答案 0 :(得分:1)
试试这个:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add respective layout
setContentView(R.layout.main_activity);
// Use package name which we want to check
boolean isAppInstalled = appInstalledOrNot("com.check.application");
if(isAppInstalled) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.check.application");
startActivity(LaunchIntent);
Log.i("Application is already installed.");
} else {
// Do whatever we want to do if application not installed
// For example, Redirect to play store
Log.i("Application is not currently installed.");
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
}
答案 1 :(得分:1)
public static void getApkPackage(PackageManager pm, String pathToApk) {
String fullPath = pathToApk;
PackageInfo info = pm.getPackageArchiveInfo(fullPath, 0);
Log.i("Package", "PackageName: " + info.packageName + ", VersionName : " + info.versionName );
}
结果:
PackageName: me.pou.app, VersionName : 1.4.73
PackageName: com.elevenbitstudios.twommobile, VersionName : 1.4.3
好的,我找到了答案。我们不需要运行exec来获取未安装&#39;的详细信息。 APK。只需使用带有路径的PackageManager.getPackageArchiveInfo,我们就可以获得apk详细信息。
答案 2 :(得分:0)
你无法在手机中找到卸载的apk的包名,但你可以手动找到卸载的apk的包名。