Я делаю школьный проект, где вы даете голосовые команды. команда, если довольно простая, в виде: «command» «parameter» пример: open facebook. У меня немного проблемы с открытием приложения, используя только имя приложения. Он всегда сбой, когда я пытаюсь открыть другое приложение с намерением. Я буду использовать ниже код:Проблемы с PackageManager. Как исправить?
private boolean spokenTextContainsAnyOfTheCommands(List<String> commands) {
for (int i = 0; i < commands.size(); i++) {
if (spokenText.contains(commands.get(i))) {
currentCommand = commands.get(i);
return true;
}
}
return false;
}
private void handleCommand() {
if (youSaidHello) {
if (spokenTextContainsAnyOfTheCommands(callCommands)) {
int idx = spokenText.indexOf(currentCommand);
String whoToCall = spokenText.substring(idx + currentCommand.length()).trim();
callThisDude(whoToCall);
}
if (spokenTextContainsAnyOfTheCommands(openCommands)) {
int idx = spokenText.indexOf(currentCommand);
String whatToOpen = spokenText.substring(idx + currentCommand.length()).trim();
Intent appIntent = getAppIntent(whatToOpen);
if (appIntent == null) {
theTextView.setText(R.string.text_view_after_negative);
}
else {
theTextView.setText(R.string.text_view_after_afirmative);
startActivity(appIntent);
}
}
}
else {
theTextView.setText(R.string.text_view_after_negative);
}
}
private Intent getAppIntent(String appName) {
String appPackage = null;
PackageManager pm;
pm = getPackageManager();
List<ApplicationInfo> appsInfo = pm.getInstalledApplications(0);
for (int i = 0; i < appsInfo.size(); i++) {
if (appsInfo.get(i).name.toLowerCase().equals(appName.toLowerCase())) {
appPackage = appsInfo.get(i).packageName;
}
}
if (appPackage == null) {
theTextView.setText(R.string.text_view_after_negative);
}
else {
theTextView.setText(R.string.text_view_after_afirmative);
}
return pm.getLaunchIntentForPackage(appPackage);
}
Нужно ли мне какое-либо разрешение в манифесте андроида?