简体   繁体   English

使用语音命令在android中打开另一个应用程序?

[英]Open another application in android using voice commands?

So I am trying to develop an application which uses voice recognition to handle a lot of events like calling any phone number, opening other applications, toggle settings, etc. 因此,我正在尝试开发一个使用语音识别来处理许多事件的应用程序,例如呼叫任何电话号码,打开其他应用程序,切换设置等。

What i have done till now is implementing the calling feature, the place where i am stuck is how to open another applications 到目前为止,我所做的是实现调用功能,卡住的地方是如何打开其他应用程序

My code till now is : 到目前为止,我的代码是:

private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech Prompt");
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(), "Error Occured Try again",Toast.LENGTH_SHORT ).show();
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);

    switch(requestCode) {
        case REQ_CODE_SPEECH_INPUT : {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                txtSpeechInput.setText(result.get(0));
                String arr[] = result.get(0).split(" ",2);
                String firstWord = arr[0];
                String secondWord = arr[1];
                switch(firstWord) {
                    case "call":
                        callPhone(secondWord);
                        break;
                    case "open":


                }

            }
        }
    }
}

Now as you can see i can use case "open" as first word and then continue with it. 现在,您可以看到我可以将大小写“ open”用作第一个单词,然后继续。 But i don't have any idea how to get the list of all apps installed currently on the phone. 但是我不知道如何获取当前在手机上安装的所有应用程序的列表。 Kindly help ? 请帮忙?

Related question: How to enable Android Open Application voice interaction 相关问题: 如何启用Android Open Application语音交互

Cited from there: "As far as I am aware, Google simply iterates over a list of installed applications and opens the corresponding application if it finds an exact match." 从那里引用:“据我所知,谷歌只是简单地遍历已安装的应用程序列表,如果找到完全匹配的应用程序,则打开相应的应用程序。”

Regards, 问候,

Get list of all application/activities installed on the device: 获取设备上安装的所有应用程序/活动的列表:

final Intent Intent = new Intent(Intent.ACTION_MAIN, null);
Intent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> packageAppsList = context.getPackageManager().queryIntentActivities(Intent, 0);

get your relevant data to open the apps on ResolveInfo, read more below: 获取您的相关数据以在ResolveInfo上打开应用程序,请阅读以下内容:

for (ResolveInfo res : packageAppsList ){
    //print it to logger etc.
    res.loadLabel(getPackageManager().toString();

https://developer.android.com/reference/android/content/pm/ResolveInfo.html https://developer.android.com/reference/android/content/pm/ResolveInfo.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM