我尝试打开我的MainActivity并直接启动语音识别的意图,只需点击我创建的小部件但没有效果。如果我点按不打开应用程序。这是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);
}
}
清单
<!-- 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>
我错过了什么吗?感谢
答案 0 :(得分:1)
您没有启动识别器意图。将voiceIntent代码移动到MainActivity onCreate ..类似如下。因此,当您单击小部件message_button时,将启动待处理的Intent MainActivity,您可以启动识别器并将结果处理回活动
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);
}
}
将您的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);
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);
}
}
还要确保在清单中将MainActivity启动模式设置为singleTop,否则您可以看到创建了多个活动实例。
android:launchMode="singleTop"