Your are not starting the Recognizer Intent. Move voiceIntent code to MainActivity onCreate .. similar as below. so when you click on the widget message_button , the pending Intent MainActivity will be launched and you can start the recognizer and handler the result back in the 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);
}
}
Change your Widget Provides as ...
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);
}
}
also make sure you set MainActivity launch mode as singleTop in manifest, else you could see multiple activity instance get created.
android:launchMode="singleTop"