簡體   English   中英

調用語音識別應用程序的小部件

[英]Widget that calls speech recognition app

我正在嘗試創建一個包含單個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);

我知道了! 我需要兩個常規意圖包含在兩個待處理的意圖中,如下所示:

// 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);

我也遇到了同樣的問題。
對不起,我沒有足夠的聲譽來發表評論。

無需使用透明活動來發送識別意圖。
喜歡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獲得結果!

我想創建像谷歌一樣的小部件。 我嘗試了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);
    }

}

為了使活動transparrent,看到這個帖子

這是完全正常的,它基於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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM