5

this is my code...

String[] show = new String[2];
RemoteViews remoteViews;
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";   

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

    remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );
    remoteViews.setTextViewText( R.id.widget_title, show[0]);
    remoteViews.setTextViewText( R.id.widget_textview, show[1]);

    Intent configIntent = new Intent(context, SuitAuto.class);
    configIntent.setAction(ACTION_WIDGET_CONFIGURE);

    Intent active = new Intent(context, WordWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    active.putExtra("msg", "Message for Button 1");

    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

    remoteViews.setOnClickPendingIntent(R.id.update, actionPendingIntent);
    remoteViews.setOnClickPendingIntent(R.id.Lilo, configPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews );
}

private String[] getString() {
    String[] hon = new String[2];
    KamusWidget ad = new KamusWidget(null);
    ad.open();
    hon = ad.getwordday();
    ad.close();
    return hon;
}

@Override
public void onReceive(Context context, Intent intent) {
    // v1.5 fix that doesn't call onDelete Action
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
        // check, if our Action was called
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
            String msg = "null";
            try {
                msg = intent.getStringExtra("msg");
            } catch (NullPointerException e) {
                Log.e("Error", "msg = null");
            }
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();


        } else {
            // do nothing
        }

        super.onReceive(context, intent);
    }
}

 }

i just want to update widget_title and widget_textview when R.id.update is clicked it should run function getString to fetch string from other class so that, the new string can be pass to the textview...

any idea?

user724861
  • 1,074
  • 3
  • 17
  • 33

2 Answers2

4

In the end of the onReceive function you should be able to just create a RemoteView and update the widget:

ComponentName name = new ComponentName(context, WordWidget.class);
int[] ids = appWidgetManager.getAppWidgetIds(name);
if(ids != null && ids.length > 0){
RemoteView remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );
remoteViews.setTextViewText( R.id.widget_title, context.getString(R.id.new_widget_title));
remoteViews.setTextViewText( R.id.widget_textview, context.getString(R.id.new_widget_text));

Intent configIntent = new Intent(context, SuitAuto.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);

Intent active = new Intent(context, WordWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Message for Button 1");

PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

remoteViews.setOnClickPendingIntent(R.id.update, actionPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.Lilo, configPendingIntent);

appWidgetManager.updateAppWidget(ids, remoteViews );
}

I believe you will need to also set the Intents: if you do not do that the buttons will no longer work once you update the RemoteView.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • 1
    it's not clear how we get the appWidgetManager "variable" in your example, I am running into a similar problem, I update the textview through a remoteview, however it doesn't seem to actually change the text. – onaclov2000 Oct 24 '11 at 01:56
  • 2
    If you have a context (any context, such as an Activity or a Service) you should be able to do `AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);`: I use this from inside a Service to update a widget that displays the value of a Bluetooth sensor. – Femi Oct 24 '11 at 05:29
  • I've been reading through http://stackoverflow.com/questions/23220757/android-widget-onclick-listener-for-several-buttons and trying to get it to work for a couple of days. Finally I found this answer with the AppWidgetManager comment above and I was able to finally get it work. Upvote! – raddevus Apr 21 '17 at 14:13
0

I believe

Intent active = new Intent(context, WordWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);

Should be

Intent active = new Intent(ACTION_WIDGET_RECEIVER);

Broadcast PendingIntents do not work if their intent was not instantiated with a simple string. setAction() does not do the same thing for some reason.

kevinmrohr
  • 821
  • 7
  • 11