So I am trying to build a virtual assistant on android. Currently i am trying to implement the logic to open another app using voice command. I saw this question and tried implementing the same code but it is giving me an exception saying Unknown package: PackageName
. So if I say "open facebook" then it says Unknown package: facebook
. I can't figure out why this code is not working!
try {
if(text.contains("open") || text.contains("launch") || text.contains("play")) {
speech = text.split(" ", 2);
String appName = speech[1];
PackageManager pm = getPackageManager();
String pkgName = pm.getInstallerPackageName(appName);
Intent launchApp = getPackageManager().getLaunchIntentForPackage(pkgName);
startActivity(launchApp);
}
} catch (Exception ex) {
Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
Edit: I got help from answer # 6 on the question which i mentioned.