Android widget not updating - has incorrect widget ID

前端 未结 3 1423
说谎
说谎 2021-01-17 16:56

I\'ve been working on a widget and making some advances (I hope) but still can\'t get it to do what I want.

I have a configurator activity which works ok. When it cl

相关标签:
3条回答
  • 2021-01-17 17:29

    You need to set a last flag in a getBroadcast: PendingIntent.FLAG_UPDATE_CURRENT

    0 讨论(0)
  • 2021-01-17 17:36

    For Updating Your Home Screen widget on Touch You need to Register a custom broadcast receiver in Manifest and add it as action with widget as:

    Step 1: Register Custom Receiver as:

    <intent-filter>
    <action android:name="com.imrankhanandroid.HomeWidgetRandomNum.ACTION_WIDGET_RANDOM"/>
    </intent-filter>
    

    Step 2: In Your Widget Class AddAction to layout as:

    public void onReceive(Context paramContext, Intent paramIntent)
        {
            //Intent
             String str = paramIntent.getAction();
             if (paramIntent.getAction().equals(ACTION_WIDGET_RANDOM)) {
                 updateWidgetState(paramContext, str);   
             }
             else
                {
                  super.onReceive(paramContext, paramIntent);
                }
        }
    

    STEP 3: In Your updateWidgetState add new action to Intent as:

    rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain);
    Intent active = new Intent(paramContext, Randomnuberwidget.class);
    active.setAction(ACTION_WIDGET_RANDOM);
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(paramContext, 0, active, 0);
    rview.setOnClickPendingIntent(R.id.ImageView_gps, actionPendingIntent);
    

    Finally you can Download Working HomeWidget Example from HERE HomeWidgetRendomNumber Which Generating Random Number On Every Click.

    0 讨论(0)
  • 2021-01-17 17:40

    In your function updateAppWidget go to this line:

    PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);
    

    ...and change it into:

    PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, widgetIntent, 0);
    
    0 讨论(0)
提交回复
热议问题