Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

android - Widget sending multiple types of intents

I am building a widget that has multiple buttons, each one sending off its own intent to a broadcast receiver. The broadcast receiver is suppose to display a Toast message based on which button was pushed. The code currently looks like this:

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

        ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            // Set the text of the buttons
            remoteViews.setTextViewText(R.id.widgetPreset1Button, prefs.getString("widget1", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset2Button, prefs.getString("widget2", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset3Button, prefs.getString("widget3", "Not set"));
            remoteViews.setTextViewText(R.id.widgetPreset4Button, prefs.getString("widget4", "Not set"));

            // Register the buttons with an OnClick event
            Intent intent1 = new Intent("myapp.SendWidgetPreset");
            intent1.putExtra("Widget", 1);
            PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset1Button, pendingIntent1);

            Intent intent2 = new Intent("myapp.SendWidgetPreset");
            intent2.putExtra("Widget", 2);
            PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset2Button, pendingIntent2);

            Intent intent3 = new Intent("myapp.SendWidgetPreset");
            intent3.putExtra("Widget", 3);
            PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset3Button, pendingIntent3);

            Intent intent4 = new Intent("myapp.SendWidgetPreset");
            intent4.putExtra("Widget", 4);
            PendingIntent pendingIntent4 = PendingIntent.getBroadcast(context, 0, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.widgetPreset4Button, pendingIntent4);

            new WidgetBroadcastReceiver();

            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
}

and the BroadcastReceiver:

public class WidgetBroadcastReceiver extends BroadcastReceiver{

    public WidgetBroadcastReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent arg1) {
        int widget = arg1.getIntExtra("Widget", -1);

        Toast.makeText(context, "Widget pressed: " + widget, Toast.LENGTH_SHORT).show();    
    }
}

My problem is it always displays Widget pressed: 4 regardless of which button is pressed. If I put the four lines intent4, intent4.putExtra(), pendingIntent4, and remoteViews.setOnClickPendingIntent() above all of the other intents, it will then always say Widget pressed: 3. In other words, whatever the last intent registration is, that is the widget displayed in the Toast message.

Anyone know why this isn't working how I want it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you need to provide seperate request code for each pendingintent ex:

PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); 
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 1/*request code*/, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...