Widget Wont Update / Redraw - java

I have a widget that loaded image from SQLite. It updated whenever I add or delete item but the image is not updated. I already try to send a broadcast to AppWidgetProvider class but it didn't work too.
class that extend AppWidgetProvider
private static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Intent intent = new Intent(context, StackWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.fav_movie_widget);
views.setRemoteAdapter(R.id.stack_view, intent);
views.setEmptyView(R.id.stack_view, R.id.empty_view);
Intent toastIntent = new Intent(context, FavMovieWidget.class);
toastIntent.setAction(FavMovieWidget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
Log.e(TAG, "onUpdate2: start" );
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.e(TAG, "onUpdate: start" );
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
activity for update
Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, FavMovieWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stack_view);
what i already try
Intent intent = new Intent(this, FavMovieWidget.class);
intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
the widget image not update
preview widget

Done by adding this on updateAppWidget.
this will triger onGetViewFactory on my service.
Random random = new Random();
intent.setType(String.valueOf(random.nextInt(1000)));
and use sendbroadcast in my activity
Intent intent = new Intent(this, FavMovieWidget.class);
intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(getApplication(), FavMovieWidget.class));
intent.putExtra(appWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);

Related

How to launch fragment with app Widget click?

How can I open the fragment in app widget? here is my AppWidgetProvider class code when i tried one more time again it is working for activities but not working for fragments is there any way to open fragment with widget clicked:
public class ApprovalWidgetProvider extends AppWidgetProvider {
private Gson gson;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
gson = new Gson();
for (int appWidgetId : appWidgetIds) {
Intent intent = new Intent(context, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.approval_widget);
views.setOnClickPendingIntent(R.id.example_widget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);
views = new RemoteViews(context.getPackageName(), R.layout.approval_widget);
views.setOnClickPendingIntent(R.id.example_widget_button2, pendingIntent2);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

Android widget listview handle click

I have seen related questions for this issue but I'am unable to figure out why this is not working for me.
I am expecting for onReceive() method in Widget Provider class to be called as the list item is clicked, but nothing happens.
Some comments are included, and they show what I have been trying as well.
Relevant code:
Main app layout has ListView defined like this:
<ListView
android:id="#+id/events_list"
style="#android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Row is defined with RelativeLayout with two nested Linear layouts, not sure if this is important to share.
getViewAt() is implemented through view factory and here's the function:
#Override
public RemoteViews getViewAt(int position) {
if (events_ack == null) {
Log.e("LIST", "getViewAt (null) events_ack");
return null;
}
RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row);
row.setTextViewText(R.id.event_name, events_ack[position].name);
row.setTextViewText(R.id.description, events_ack[position].description);
row.setTextViewText(R.id.publicTV, (events_ack[position].is_public == 0) ? "Public: False" : "Public: True");
row.setTextViewText(R.id.timeTV, events_ack[position].time);
row.setViewVisibility(R.id.price, View.GONE);
// download and set image BONUS QUESTION- does not work through AsyncTask using future insted (images are downloaded but not shown).
row.setImageViewBitmap(R.id.image, pic);
Bundle extras = new Bundle();
extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
// Make it possible to distinguish the individual on-click
// action of a given item
row.setOnClickFillInIntent(R.id.events_list, fillInIntent);
//extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
//i.putExtras(extras);
//i.setAction(CasusWidgetProvider.ITEM_CLICK);
//row.setOnClickFillInIntent(R.layout.casus_widget, i); //event_name?
// ??
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
appWidgetManager.updateAppWidget(appWidgetId, rv);
//appWidgetManager.updateAppWidget(appWidgetId, row);
return row;
}
Widget Provider class has onUpdate() method:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.w("CASUS", "UPDATE function called");
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent);
Intent clickIntent=new Intent(context, WidgetService.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
clickIntent.setAction(ITEM_CLICK);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.events_list, clickPI);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.events_list); //?
clickIntent = new Intent(context, CasusWidgetProvider.class);
clickIntent.setAction(UPDATE_LIST);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(context, 0, clickIntent, 0);
rv.setOnClickPendingIntent(R.id.update, pendingIntentRefresh);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
fetchData(context, appWidgetIds);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
I have figured it out. onUpdate() function in widget provider class was modified to look like this:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
fetchData(context, appWidgetIds);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
In getViewAt() function on widget factory this line:
row.setOnClickFillInIntent(R.id.events_list, fillInIntent);
Was modified to match correct ID:
row.setOnClickFillInIntent(R.id.row_layout, fillInIntent);
The click handler template is set in class that does the HTTP request, basically:
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.casus_widget);
Intent svcIntent = new Intent(this.getApplicationContext(), WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
svcIntent.putExtra("events", events);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(widgetId, R.id.events_list, svcIntent);
Intent clickIntent = new Intent(this.getApplicationContext(), CasusWidgetProvider.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, allWidgetIds);
clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
clickIntent.setAction(CasusWidgetProvider.ITEM_CLICK);
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.events_list, toastPendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
Hope this might help..

Android widget talk to service and service talk to widget

I have an Android app whose service is started when app starts and when app is killed, service is also destroyed.
I have a home screen widget starting alongside with app. It has 3 imageviews - "Play","Next","Previous".
On press of any of these imageview in widget, they need to talk to service and service talks to library etc.
When service has an answer, it responds to widget with status OK or NO. Based on the response, I need to update the imageviews.
When the app is killed, I need to gray out my widget fully. In this situation, when I click on the greyed out widget, I need to start the app. Could anyone help me please
how I could achieve this.
Here is my widget code -
public class MyWidgetProvider extends AppWidgetProvider {
private Intent serviceIntent;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName componentName;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
componentName = new ComponentName(context, ControlScreenWidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.PLAY,getPendingSelfIntent(context, "PLAY",componentName));
remoteViews.setOnClickPendingIntent(R.id.NEXT,getPendingSelfIntent(context, "NEXT",componentName));
remoteViews.setOnClickPendingIntent(R.id.PREVIOUS,getPendingSelfIntent(context, "PREVIOUS",componentName));
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName componentName;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
componentName = new ComponentName(context, MyWidgetProvider.class);
if ("PLAY".equals(intent.getAction())) {
remoteViews.setOnClickPendingIntent(R.id.PLAY,getPendingSelfIntent(context, "PLAY",componentName));
// Dont know what to do here
}
else if ("NEXT".equals(intent.getAction())) {
remoteViews.setOnClickPendingIntent(R.id.lock_door,getPendingSelfIntent(context, "NEXT",componentName));
// Dont know what to do here
}
else if ("PREVIOUS".equals(intent.getAction())) {
remoteViews.setOnClickPendingIntent(R.id.lock_door,getPendingSelfIntent(context, "PREVIOUS",componentName));
// Dont know what to do here
}
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
protected PendingIntent getPendingSelfIntent(Context context, String action, ComponentName componentName) {
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(action);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, componentName);
return PendingIntent.getService(context, 0, intent, 0);
}
}

clickable ImageView in Widget?

I have this widget in android, that displays an image and gets updated periodically by a broadcast receiver. Now I want to give the user the ability to start a configuration screen (preference activity) by clicking on the widget. Here it goes:
The widget provider class:
public class MyProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
for (int widgetId : appWidgetIds) {
// setting onClickListener - at least i TRY to
Intent onClickIntent = new Intent(context, Preferences.class);
PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);
rViews.setOnClickPendingIntent(R.id.graph, onClickPendingIntent);
appWidgetManager.updateAppWidget(widgetId, rViews);
}
}
}
The updates of the widget are triggered from here:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName myWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
int[] allMyWidgetIds = appWidgetManager.getAppWidgetIds(myWidget);
for (int widgetId : allMyWidgetIds)
{
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// update the widgets content, i.e. picture
appWidgetManager.updateAppWidget(widgetId, rViews);
}
// schedule next update
Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, Config.UPDATE_RATE);
Date d = new Date(c.getTimeInMillis());
Intent timerIntent = new Intent(context.getApplicationContext(), TimeIntervalReceiver.class);
timerIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent timerPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, timerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
aManager.set(AlarmManager.RTC, d.getTime(), timerPendingIntent);
}
}
Just to mention it: the Preference.java-Activity is registered in the Manifest and can be started correctly (I verified this by setting it as the startup activity).
Anyhow, the onclick-functionality does not work at all :(
I also tried to move the code to the "MyReceiver"-class which updates the widgets: nothing :(
I already looked at:
* Android: Clickable imageview widget
* http://www.vogella.de/articles/AndroidWidgets/article.html
However, this didn't help at all :(
anyone here who can tell me where my problem is?
Try changing this line:
PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);
to
PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);
PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);
Retrieve a PendingIntent that will start a new activity
http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)
Plz refer link for full reference

Why doesn't my app widget update in API 3 or 4?

I'm working on an android widget and it works great in API Level 5 or greater. It's not supported at all in API Level 1 or 2. It should work absolutely fine in 3 and 4 but for some reason the widget doesn't update.
The onUpdate method gets called and executes without errors; however, in 3 and 4 it doesn't change the text of the widget. I'm pretty much at a loss. Any thoughts?
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
DataAccess helper = new DataAccess(context);
String text = helper.getCurrentText();
helper.close();
if (text != null)
views.setTextViewText(R.id.widget_text, text);
Intent intent = new Intent(context, WidgetDetailsActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget, pending);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
Try this:
At the end of your update method you need to call the super.
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
DataAccess helper = new DataAccess(context);
String text = helper.getCurrentText();
helper.close();
if (text != null)
views.setTextViewText(R.id.widget_text, text);
Intent intent = new Intent(context, WidgetDetailsActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget, pending);
appWidgetManager.updateAppWidget(appWidgetIds, views);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
You need to start service on update method and put your all code in service also,here I give onReceive and service demo
context.startService(new Intent(context, UpdateService.class));
public static class UpdateService extends Service {
#Override
public void onStart(Intent intent, int startId) {
// Build the widget update for today
RemoteViews updateViews = buildUpdate(this);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this,
Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
#Override
public void onReceive(Context context, Intent intent) {
final RemoteViews remoteViews = buildUpdate(context);
ComponentName cn = new ComponentName(context, Widget.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
super.onReceive(context, intent);
}
where buildUpdate is method for your widget updating code

Categories

Resources