Search code examples
androidandroid-appwidget

Remote View update issue


I am taking datas from the users and storing into the database. My database takes only one value. So each time the user enters the database the database is updated. I show these datas in the textview of the AppWidget.For that I use this code:

public void onUpdate(Context context, android.appwidget.AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    //Get all ids
    ComponentName thisWidget = new ComponentName(context.getPackageName(),SmsSchedulerWidget.class.getName());

    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);


    //the datas to be shown are fetched from the database
    DatabaseManager dbManager = new DatabaseManager(context);
    dbManager.open();
    contactNumber = dbManager.fetchContactNumber();
    Log.i("contactNumber",contactNumber);
    date = dbManager.fetchDate();
    Log.i("date",date);
    message = dbManager.fetchMessage();
    Log.i("message",message);
    status = dbManager.fetchStatus();
    Log.i("status", status);
    dbManager.closeDatabase();
    //Build the intent to call the service

    //it creates the UI for the given app widget
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.schdulesms_appwidget_layout);


    remoteViews.setTextViewText(R.id.to_appwidget_saved_data,
            contactNumber);
    remoteViews.setTextViewText(R.id.date_appwidget_saved_data, date);
    remoteViews.setTextViewText(R.id.status_appwidget_saved, status);
    remoteViews.setTextViewText(R.id.message_appwidgset_saved_data,
            message);

    //to start the activity with the click on the layout
    Intent clickIntent = new Intent(context.getApplicationContext(),MainActivity.class);
    clickIntent.setFlags(clickIntent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent= PendingIntent.getActivity(context, 0, clickIntent, 0);
    remoteViews.setOnClickPendingIntent(R.id.full_widget, pendingIntent);

    //to update the BroadCast Receiver so that it holds the current values in the layout
    Intent updateIntent =new Intent(context,SmsSchedulerWidget.class);

    updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    updateIntent.setComponent(thisWidget);

    PendingIntent pendingIntentForUpdate = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.logoBtn, pendingIntentForUpdate);

    appWidgetManager.updateAppWidget(thisWidget, remoteViews);

    }

My app widget should update itself after the button click and as well as the user has entered new values into the database at run time.But the app widget only updates itself after the appwidget is uninstalled from the screen and then installed again or the program is rerun.Why is this happening? How to update my app widget after the click of the button?????


Solution

  • To manually update your AppWidget, you must send it a Broadcast and receive it in your AppWidgetProvider's onReceive() method. From the onReceive() you should extract all the values you need from the broadcast Intent and then call the onUpdate() method manually. Hope this helps.