Satheesh
Satheesh

Reputation: 1730

How to automatically update widget in home screen

I have completed one task application in android.And now i create one widget for this application.In my widget i have display list of task for today,which is working correctly.My problem is when i go to my application and add some task in today and then back to the home screen the widget having only old data instead of new data and no modification...please any one help me....

My RemoteFactory class:

public class TaskItemStatus implements RemoteViewsService.RemoteViewsFactory {
Context context;
int appWidgetId;
String statusRemainingTask="false";
String[] items;
private final String TAG = "CalendarViewSample:"
        + this.getClass().getName();

public TaskItemStatus(Context context, Intent intent) {
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
    getData();
}

@SuppressLint("SimpleDateFormat")
private void getData() {
    List<String> listTask=new ArrayList<String>();
    Taskdatabase objTaskDb = new Taskdatabase(this.context);
    objTaskDb.Open();
    Calendar calendarToday = Calendar.getInstance();
    SimpleDateFormat simpledateFormat = new SimpleDateFormat("dd-MM-yyyy");
    String dateToday = simpledateFormat.format(calendarToday.getTime());
    listTask.addAll(objTaskDb.fetchTodayRemainTask(dateToday, statusRemainingTask));
    Log.i(TAG,"ListTask:"+listTask.toString());
    items=new String[listTask.size()];
    items=listTask.toArray(items);
}

@Override
public int getCount() {
    return (items.length);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public RemoteViews getLoadingView() {
    return null;
}
@Override
public RemoteViews getViewAt(int position) {
    RemoteViews row = new RemoteViews(context.getPackageName(),
            R.layout.widgetrow);
    row.setTextViewText(android.R.id.text1, items[position]);
    Intent i = new Intent();
    //Bundle extras = new Bundle();
    //extras.putString(WidgetTaskSchedular.EXTRA_WORD, items[position]);
    //i.putExtras(extras);
    row.setOnClickFillInIntent(android.R.id.text1, i);
    return (row);
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public boolean hasStableIds() {
    return (true);
}

@Override
public void onCreate() {

}

@Override
public void onDataSetChanged() {

}

@Override
public void onDestroy() {

}

}

WidgetProvider:

 public class WidgetTaskSchedular extends AppWidgetProvider {
static int ID;
    static final int[] sameid=new int[1]; 
    public static String EXTRA_WORD=
            "com.capsone.testing.calendar.WORD";
@Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if(intent.getAction().equals("update_widget"))
        {
        Log.i(TAG,"AppWidgetIds:"+ID);


        for(int i=0;i<1;i++)
        {
            sameid[i]=ID;
            Log.i(TAG,"SameId:"+sameid[i]);
            onUpdate(context, AppWidgetManager.getInstance(context),sameid);
        }

        }
    }





    @SuppressWarnings("deprecation")
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
ID=appWidgetIds[i];

         for (int i=0; i<appWidgetIds.length; i++) {
              Log.i("Widget","WidgetId:"+appWidgetIds.length);
              Intent intentWidgetService=new Intent(context, WidgetService.class);
              intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
              intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME)));

              RemoteViews remoteView=new RemoteViews(context.getPackageName(),
                                                  R.layout.widgetlayout);

              remoteView.setRemoteAdapter(appWidgetIds[i], R.id.listWidget,
                                      intentWidgetService);


              Intent clickIntent=new Intent(context, ActionBarActivity.class);
              PendingIntent clickPendingIntent=PendingIntent
                                      .getActivity(context, 0,
                                                    clickIntent,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
              remoteView.setPendingIntentTemplate(R.id.listWidget, clickPendingIntent);
              ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
              appWidgetManager.updateAppWidget(component, remoteView);
            }

    }
    }

Upvotes: 0

Views: 8258

Answers (2)

Satheesh
Satheesh

Reputation: 733

AppWidgetProvider class:

ComponentName component = new ComponentName(context, WidgetTaskSchedular.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listWidget);
appWidgetManager.updateAppWidget(component, remoteView);

RemoteView class:

@Override
public void onDataSetChanged() {
    // Just copy and paste you getdata() coding here
}

Upvotes: 4

Dhruv
Dhruv

Reputation: 185

One way to achieve this is when you save the new task in your application just send a broadcast with a custom intent to indicate the change in the underlying database. Add a receiver to this broadcast in your WidgetTaskSchedular class and in on receive method call the onUpdate method to re-populate data in the widget. Somewhat like this:

public void onReceive(Context context, Intent intent) {
    System.out.println("On receive function");
    if (intent.getAction().equals("com.android.myapp.myBroadcast")) {
        System.out.println("There is an update from app ");
        //re populate data or in onUpdate
        onUpdate(context, AppWidgetManager.getInstance(context), IDs);
    }
    super.onReceive(context, intent);
}

PS:Save Ids as a static field or something.I took in the IDs in a static array of integers which I populatedin the onUpdate method,you can also try replacing that part with the following code: RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

        // Update  - here like for example as below
        remoteViews.setTextViewText(R.id.yourTextID, "My updated text");

        // Trigger widget layout update
        AppWidgetManager.getInstance(context).updateAppWidget(
                new ComponentName(context, Widget.class), remoteViews);

Upvotes: 1

Related Questions