gpt4 book ai didi

java - 通过PendingIntent开放应用程序并直接进行语音识别

转载 作者:行者123 更新时间:2023-12-01 13:13:34 25 4
gpt4 key购买 nike

我尝试打开我的 MainActivity 并直接启动语音识别 Intent ,只需点击我创建的小部件,但没有效果。如果我点击不打开应用程序。这是Widget类

public class MyWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent configIntent = new Intent(context, MainActivity.class);

PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

remoteViews.setOnClickPendingIntent(R.id.message_button, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

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, configPendingIntent);
//updateView(context);

}

}

list

<!-- Widget -->
<receiver
android:icon="@drawable/icon"
android:label="IntentProva"
android:name=".MyWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/batterywidgetinfo" />
</receiver>

以及布局

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView

android:id="@+id/message_button"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/icon" />
</LinearLayout>

我是不是错过了什么?谢谢

最佳答案

您没有启动识别器 Intent 。将 voiceIntent 代码移至 MainActivity onCreate .. 类似如下。因此,当您单击小部件 message_button 时,将启动待处理的 Intent MainActivity,您可以启动识别器并在 Activity 中处理结果

 private int SPEECH_REQUEST= 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

boolean isFromWidget = false;
Bundle extras = getIntent().getExtras();
if(extras != null && extras.get("widget") != null)
{
isFromWidget = Boolean.valueOf(extras.get("widget").toString());
}
if(isFromWidget) {
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);
startActivityForResult(voiceIntent, SPEECH_REQUEST_CODE);
}
}

将您的小部件提供更改为...

public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent configIntent = new Intent(context, MainActivity.class);
configIntent.putExtra("widget",true);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.message_button, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}

还要确保在 list 中将 MainActivity 启动模式设置为 singleTop,否则您可能会看到创建了多个 Activity 实例。

 android:launchMode="singleTop"

关于java - 通过PendingIntent开放应用程序并直接进行语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22644109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com