首页 文章

调用语音识别应用程序的小部件

提问于
浏览
13

我正在尝试创建一个包含单个ImageView的小部件,当单击它时,它启动语音识别应用程序 . 我从来没有使用过小部件和待定的意图,所以我很困惑:如何创建一个未决的意图来启动语音识别活动?

我尝试过类似的东西,当然,它失败了:

Intent intent = new Intent();
   Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
   voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
     RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
   voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT,
     "Speech recognition demo");
   voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, voiceIntent);
   PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
     intent, 0);
   RemoteViews views = new RemoteViews(context.getPackageName(),
     R.layout.main);
   views.setOnClickPendingIntent(R.id.button, pendingIntent);

4 回答

  • 1

    我知道了!我需要两个常规意图包含在两个待处理的意图中,如下所示:

    // this intent points to activity that should handle results
    Intent activityIntent = new Intent(context, ResultsActivity.class);
    // this intent wraps results activity intent
    PendingIntent resultsPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
    
    // this intent calls the speech recognition
    Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
    voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, resultsPendingIntent);
    
    // this intent wraps voice recognition intent
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, voiceIntent, 0);
    rv.setOnClickPendingIntent(R.id.btn, pendingIntent);
    
  • 9

    我也遇到了同样的问题 .
    抱歉,我没有足够的声誉发表评论 .

    无需使用透明活动来发送识别意图 .
    喜欢zorglub76的答案

    Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
    voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, resultsPendingIntent);
    

    识别结果只是在 resultingPendingIntent 的额外
    所以你需要做的就是:

    ResultsActivity.onCreate()

    ArrayList<String> voiceResults = this.getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
    

    小心 NullPointerException ,你将得到ArrayList的结果!!

  • 4

    我想创建像谷歌一样的小部件 . 我尝试了zorglub76解决方案,但我无法获得结果的声音......

    我通过创建一个虚拟的透明活动解决了这个问题,该活动可以端到端地处理语音识别 .

    它的工作方式如下:Widget-> VoiceRecognitionStarterActivity-> RecognizerIntent-> VoiceRecognitionStarterActivity.onActivityResult .

    我的小部件类:

    public class MyWidgetProvider extends AppWidgetProvider {
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
    
        // Get all ids
        ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    
            Intent activityIntent = new Intent(context, VoiceRecognitionStarterActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
            remoteViews.setOnClickPendingIntent(R.id.mic_image, pendingIntent);
    
            activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(context.getString(R.string.search_url)));
            pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
            remoteViews.setOnClickPendingIntent(R.id.search_box_image, pendingIntent);
    
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
    
        }
        }
    }
    

    我的透明活动:

    public class VoiceRecognitionStarterActivity extends Activity
    {
        private static final String TAG = "VoiceRecognitionStarterActivity";
        private int SPEECH_REQUEST_CODE = 1;
    
        @Override
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sendRecognizeIntent();
    }
    
    private void sendRecognizeIntent()
    {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to search");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
        startActivityForResult(intent, SPEECH_REQUEST_CODE);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == SPEECH_REQUEST_CODE)
        {
            if (resultCode == RESULT_OK) {
                Log.d(TAG, "result ok");
                Intent searchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.search_url)));
                startActivity(searchIntent);
                finish();   
             } else {
                Log.d(TAG, "result NOT ok");
                finish();
            }
    
        }
    
        super.onActivityResult(requestCode, resultCode, data);
        }
    
    }
    

    要使活动透明,请参阅this post

  • 0

    这是完全正常的,它基于Android SDK中的ListView小部件 . 它不是特别适用于小部件,但我确信您可以修改它以使其适用于小部件 .

    创建一个名为SearchActivity的活动:

    // CustomSearch (View) & ISearch (Interface) are objects that I created and are irrelevant
    public class SearchActivity extends AppCompatActivity implements ISearch
    {
        // Variables
        private CustomSearch mSearchView;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search);
    
            mSearchView = (CustomSearch)findViewById(R.id.search);
            mSearchView.setPendingComponentName(getComponentName());
            mSearchView.setSearchListener(this);
        }
    
        @Override
        protected void onNewIntent(Intent intent)
        {
            if (Intent.ACTION_SEARCH.equals(intent.getAction()))
            {
                String query = intent.getStringExtra(SearchManager.QUERY);
                Log.i("SEARCH >", "You said: " + query);
            }
        }
    }
    

    将活动添加到AndroidManifest.xml

    <activity
        android:name=".activities.SearchActivity"
        android:label="@string/app_name"
        android:theme="@style/CustomTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
        </intent-filter>
    </activity>
    

    在您的自定义窗口小部件/视图中:

    buttonVoice.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v)
        {
            // Get activity from either SearchableInfo or ComponentName
            ComponentName searchActivity = mComponentName;
    
            // Wrap component in intent
            Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
            queryIntent.setComponent(searchActivity);
    
            // Wrap query intent in pending intent
            PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent, PendingIntent.FLAG_ONE_SHOT);
    
            // Create bundle now because if we wrap it in pending intent, it becomes immutable
            Bundle queryExtras = new Bundle();
    
            // Create voice intent
            Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZER_SPEECH);
            voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak");
            voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity
            voiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            // Wrap the pending intent & bundle inside the voice intent
            voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
            voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);
    
            // Start the voice search
            getContext().startActivity(voiceIntent);
        }
    }
    

相关问题