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...
Related
My widget wont update if I change the size of it.
What am I doing wrong here? It seems like onAppWidgetOptionsChanged is not working properly.
It wont update if I update the width and height in the widget provider. I get the same width and height every time.
Here is my code for the class:
public class wimWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Toast.makeText(context, "onUpdate ", Toast.LENGTH_LONG).show();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
Intent intent = new Intent(context.getApplicationContext(),
UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
Log.v("wim", "onUpdate-" + intent);
// To react to a click we have to use a pending intent as the
// onClickListener is
// excecuted by the homescreen application
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//remoteViews.setOnClickPendingIntent(R.id.imagewidget, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widget116, pendingIntent);
// Finally update all widgets with the information about the click
// listener
ComponentName watchWidget;
watchWidget = new ComponentName( context, wimWidget.class );
// appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
if(options!=null){
//int nwidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, options);
}
}
// Update the widgets via the service
context.startService(intent);
}
#Override
public void onAppWidgetOptionsChanged (Context context, AppWidgetManager
appWidgetManager, int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
Toast.makeText(context, "Oppdatert - onAppWidgetOptionsChanged - minWidth: " + minWidth, Toast.LENGTH_LONG).show();
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
private void updateWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Bundle options) {
Toast.makeText(context, "updateWidget", Toast.LENGTH_SHORT).show();
// Do whatever you need to do to the widget. Include any customization based
// on the size, included in the options. Be sure to have a graceful fallback if
// no valid size data is included in the bundle.
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
//Toast.makeText(context, "onDeleted", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
context.stopService(serviceIntent);
super.onDeleted(context, appWidgetIds);
}
}
and here is the provider:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="50.0dp"
android:minHeight="110.0dp"
android:updatePeriodMillis="111600"
android:initialLayout="#layout/widget"
/>
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.
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.
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.