I have a problem with my App Widget when i try to update the content from my application. My widget contains a listview.
The listview works fine and it updates every 30 minutes, but I also need to update it when i make changes in the application. Here is my code:
public class WidgetListProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
ComponentName component;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
component=new ComponentName(context,WidgetListProvider.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listViewWidget);
appWidgetManager.updateAppWidget(component, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_list_layout);
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.listViewWidget, svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
return remoteViews;
}
}
put this code in your application where you are updating content.
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetIds[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetListProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listViewWidget);
I also need to update it when i make changes in the application
You can get an AppWidgetManager whenever you need to, via the static getInstance() method. So long as you keep track of which app widget ID you need to update, you can largely go through your existing code when other work in your app requires your app widget contents to update.
Related
I'm creating a Widget with ListView on it. Displaying the result from the Web Service in the ListView is okay. I created a class which is my WidgetService that extends to RemoteViewsService and on RemoteViewsFactory I call my ListViewProvider that has the Web Service. I added a refresh button that when clicked it will call the WidgetService again to created the list view.
Here is my code in my WidgetProvider
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
//ListView
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget, svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
//REFRESH
PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, svcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.ivRefreshWidget, updatePendingIntent);
return remoteViews;
}
As you can see in the refresh part I tried using PendingIntent to call the WidgetService but when testing it doesn't refresh the list.
Thank you for the help in advance.
In my onUpdateI set an Action to be called in my onReceive, and also put the appWidgetIds to be used as well in the onReceive
Intent refreshIntent = new Intent(context, WidgetProvider.class);
refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
then in onReceive I call the onUpdate
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
Bundle extras = intent.getExtras();
if (extras != null) {
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds != null && appWidgetIds.length > 0) {
Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
}
}
}
arrayList = new ArrayList<AllPostsProvider>();
arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());
and when click refreash button
arrayList.clear();
arrayList = new ArrayList<AllPostsProvider>();
arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());
I have seen several questions regarding how to update widgets but nothing is helping me with my issue.
I created a widget that has a textview that I want to dynamically update upon widget creation.
I have a configuration activity that is called when adding the widget on the screen. I store the value in the sharedpreferences and retreive it onUpdate. The problem is that the widget doesn't get updated. Any idea how it can be done? I do not want to update the textview after a click on the widget, just get the correct text to be displayed on creation.
public class AndroidWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context,
AndroidWidget.class);
int[] allWidgetInstancesIds = appWidgetManager
.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetInstancesIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// Create an intent that when received will launch the PopUpActivity
Intent intent = new Intent(context, AndroidWidget.class);
intent.setAction(SHOW_POPUP_DIALOG_ACTION);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
widgetId, intent, 0);
// Set up the onClickListener of the widget
remoteViews.setOnClickPendingIntent(R.id.myText, pendingIntent);
SharedPreferences prefs = context.getSharedPreferences(
String.valueOf(widgetId), Context.MODE_PRIVATE);
remoteViews.setTextViewText(R.id.myText,
prefs.getString("storedtext", null));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
This actually updates the textview but only after clicking or creating another widget.
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//set widget id
ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intentService = new Intent(context, WidgetService.class);
intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent clickIntent = new Intent(context, MainActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setTextViewText(R.id.currency, " Dollar");
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
This code works for me. You may try this.
For below code...
below code is for the android widget
the same code is on this link Clickhere
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds)
{
updateAppWidget(context, appWidgetManager, widgetId);
}
}
protected static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
//Inflate layout.
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.time_weight_layout);
//Update UI.
remoteViews.setTextViewText(R.id.tvTime, Utility.settemp());
remoteViews.setTextViewText(R.id.tvTitle, Utility.gettitle()); //When user click on the label, update ALL the instances of the widget.
Intent labelIntent = get_ACTION_APPWIDGET_UPDATE_Intent(context);
labelIntent.setData(Uri.withAppendedPath(Uri.parse("abc" + "://widget/id/"), String.valueOf(appWidgetId)));
PendingIntent labelPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, labelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.tvTime, labelPendingIntent);
Log.d("updateAppWidget", "Updated ID: " + appWidgetId);
//Call the Manager to ensure the changes take effect.
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
protected static Intent get_ACTION_APPWIDGET_UPDATE_Intent(Context context)
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), TimeWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
Intent intent = new Intent(context, MainActivity.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
return intent;
}
here Main activity is configure activity I want to start MainActivity on click of widget
Here's a youtube link for the same.
It's only 6 min tut.
I am trying to manually update my ListView in the appwidget when I make some data changed in the activity, and I can do it successfully when I first time install the app on the phone.
But after I leave the app and re-enter it again, I can't update my remoteviews.
I call the AppWidgetUpdater to help me update the remoteviews:
AppWidgetUpdater appWidgetUpdater = new AppWidgetUpdater(context);
appWidgetUpdater.updateAppWidget();
AppWidgetUpdater:
public void updateAppWidget(){
ComponentName appWidget = new ComponentName(context, SimpleTODOAppWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(appWidget);
Intent intent = new Intent(context, SimpleTODOAppWidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
context.sendBroadcast(intent);
}
AppWidgetProvider:
public class SimpleTODOAppWidgetProvider extends AppWidgetProvider {
private Context context;
private AppWidgetManager appWidgetManager;
private int[] appWidgetIds;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
Log.d("TEST", appWidgetIds.toString() + " from provider");
}
}
#SuppressWarnings("deprecation")
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget_main);
Intent svcIntent = new Intent(context, ListViewAdaptorService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.appwidget_listview,
svcIntent);
remoteViews.setEmptyView(R.id.appwidget_listview, R.id.appwidget_addbutton);
return remoteViews;
}
}
Finally I find what the problem is.
I should use:
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.appwidget_listview);
instead of:
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
to update my ListView. I should have done some work to get familiar with ListViewAdaptor before I used Adapter in the AppWidget...
OK so I have an android widget and I've been having trouble with the android widget updating. I've used some simple hard coded setTextViewText but nothing happen when I debug the app..
My code:
public class Widget extends AppWidgetProvider {
RemoteViews views;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int N = appWidgetIds.length;
views = new RemoteViews(context.getPackageName(), R.layout.widget);
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
String alarm = Settings.System.getString(context.getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);
views.setTextViewText(R.id.tvAlarm, alarm);
views.setTextViewText(R.id.tvNextAlarm, "qwertyuiop");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
I know some of the code is wrong, such as the alarm and I am trying to incorporate a battery status somehow but I've been told that a BroadcastReceiver isn't the way forward so at the moment I've commented it out.
But the main problem is the updating the widget to change a TextView isn't working...
I've just tried this code in one of my widgets and it works fine:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// Get ids of all the instances of the widget
ComponentName widget = new ComponentName(context, MediaWidgerProvider.class);
int[] widgetIds = appWidgetManager.getAppWidgetIds(widget);
for (int widgetId : widgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget);
remoteViews.setTextViewText(R.id.widget_text, "Hello");
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}