我知道要以编程方式打开应用程序,我们可以这样做:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.name");
startActivity(launchIntent);
但是不知道包名怎么办;只是应用程序名称?
谢谢!
编辑:我试图通过打开应用程序并检查日志来获取包名,但包名没有显示。
最佳答案
也许这样的事情会有所帮助,但我必须补充一点,这是一种糟糕的方法,因为不同应用程序的应用程序名称可能相同,并且某些应用程序(如“联系人”)根据语言集具有不同的标签
/**
*
* @param appName The target app to launch, if there are multiple apps with same name, the first found will be launched
* @throws IllegalArgumentException If the target app was not found
*/
public void runApp(String appName) throws IllegalArgumentException {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
for ( ResolveInfo info : getPackageManager().queryIntentActivities( mainIntent, 0) ) {
if ( info.loadLabel(getPackageManager()).equals(appName) ) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(info.activityInfo.applicationInfo.packageName);
startActivity(launchIntent);
return;
}
}
throw new IllegalArgumentException("Application not found!");
}
关于android - 如果我不知道包名,如何以编程方式打开应用程序 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30169390/