ActivityNotFoundException: No Activity found to ha

2019-05-05 10:26发布

问题:

I'm trying to make an app using the voice recognizer. This is a piece of my code:

public class Habla extends Activity{

   private static int code = 123;
   ...
   public void escuchar()
   {
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);

       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, numberResults);
       startActivityForResult(intent, code);
    }
...
}

The error is the following:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.speech.action.RECOGNIZE_SPEECH (has extras) }

Why does it happen? .Habla is a class which is run pushing a button in .MainActivity, so the AndoridManifest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.emiliomorillanieto.practica3" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Habla"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
   </application>
</manifest>

回答1:

The reason is voice search app from google is missing on the device you are using. You can solve the problem by manually installing it on your device. But there is another way to do so. That's opening the link of the app in a webview like following

try {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");

    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

} catch(ActivityNotFoundException e) {
    Intent your_browser_intent = new Intent(Intent.ACTION_VIEW,         

    Uri.parse("https://market.android.com/details?id=APP_PACKAGE_NAME"));
    startActivity(your_browser_intent);
}

You can also do it by coding and not using webview but that's a lot of work and you need to write a whole bunch of code. So, I think using a webview is pretty much ok.



回答2:

You should check if a recognition app is installed first:

PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
    if (infos.size() > 0) {
         //Then there is application can handle your intent
    }else{
         //No Application can handle your intent
    }


回答3:

If you have a device with custom ROM:

Download: Google App.apk [https://www.apkmirror.com/apk/google-inc/google-search/] or .app if you have acess to Google Play Store.

Install using adb command "adb install path_to_apk" [https://developer.android.com/studio/command-line/adb.html?hl=en#move])

If it does not work, download also: Google Play Services.apk

I know this question is over 1 year, but I had problems with a robot (with Android OS 4.4.4 API 19 and custom ROM) and having used RecognizerIntent helped me a lot.