質問

I try to open my MainActivity and directly start the intent for speech recognition just tapping the widget i created but no works. If i tap not opens the app. This is the Widget class

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

    }

}

The manifest

<!-- 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> 

And the layout

   <?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>

Have i missed something? Thanks

役に立ちましたか?

解決

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"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top