Widget frozen after HomeScreen restart - android

I've created widget which can be multiple instantiated with different settings stored in SharedPreferences. After solving initial problem with non-updating widgets directly after Configuration Activity (Multiple, configurable widgets - update issue ) everything seems to be working fine. But. When Home Screen application is killed/rebooted (Trebuchet in my case, Android 4.3 & 4.2.2) widget is not refreshed and onClick listeners are gone (onUpdate action is not triggered). I see only non-responsive, empty WigdetView with default, non-updated layout from xml. What is more weird - after phone reboot or after add another instance of my widget - everything works fine again.
I've tried many different ways to solve this strange issue (like single reference to RemoteViews, use Service instead of AsyncTask etc) - but none of this ideas solved the problem..
There are a lot of important logic in code, shortcut:
AppWidgetProvider:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// ASYNCTASK WAY
new DownloadTask(context, appWidgetManager).execute("http://sampleurl/");
/*
// OR SERVICE WAY
// SERVICE LOGIC IS ALMOST THE SAME AS IN DownloadTask
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.startService(intent);
*/
}
#Override
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
int mAppWidgetId=0;;
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
String action = intent.getAction();
if (action.equals("PreferencesUpdated")) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.menuLayout, View.GONE);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
int[] appWidgetIds = new int[] {mAppWidgetId};
onUpdate(context, appWidgetManager, appWidgetIds);
}
else if (action.equals("ShowMenu")) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.menuLayout, View.VISIBLE);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
}
super.onReceive(context, intent);
}
DownloadTask
public DownloadTask(Context context, AppWidgetManager appWidgetManager) {
this.context = context;
this.appWidgetManager = appWidgetManager;
}
#Override
protected String doInBackground(String... url) {
// 1. DOWNLOAD FILE
// 2. PROCESS FILE, ADD TO DATABASE, ETC
// 3. UPDATE ALL WIDGETS
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
int myPref= settings.getInt("myPref"+widgetId, -1);
if(myPref!= -1){
Log.d("DownloadTask", "widget id:"+widgetId);
updateWidget(db, widgetId, selectedStation);
}
}
db.close();
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
//SHOW PROGRESSBAR (LOADER) ON ALL WIDGETS
//ADD onClicks
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
views.setViewVisibility(R.id.progressLayout, View.VISIBLE);
views.setOnClickPendingIntent(R.id.linearLayout1, getMenuPendingIntent(context, widgetId));
views.setOnClickPendingIntent(R.id.refreshButton, getRefreshPendingIntent(context, widgetId));
views.setOnClickPendingIntent(R.id.settingsButton, getSettingsPendingIntent(context, widgetId));
appWidgetManager.updateAppWidget(widgetId, views);
}
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//HIDE PROGRESSBAR (LOADER) ON ALL WIDGETS
ComponentName thisWidget = new ComponentName(context, MyWidget.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setViewVisibility(R.id.progressLayout, View.INVISIBLE);
appWidgetManager.updateAppWidget(thisWidget, views);
if (exception){
Toast.makeText(context, context.getString(R.string.net_exc), Toast.LENGTH_LONG).show();
}
}
public void updateWidget(MySQLiteHelper db, int widgetId, int myPref){
// UPDATE SINGLE WIDGET
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_lay);
views.setTextViewText(R.id.someText, "someText");
// ETC
appWidgetManager.updateAppWidget(widgetId, views);
}
I really hope that you guys can help me, cause I'm stuck.
ps. This is expected behaviour that all widgets are refreshed together.

After having had a similar problem, my widget not working after restart of the launcher/homescreen, here a possible solution:
Whenever accessing your RemoteViews, set the .setOnClickPendingIntent(...) again.
So basically speaking, in your onReceive() where you modify your RemoteViews add the corresponding lines to add the PendingIntents, too, and everywhere else where you might modify those.
PS:
You can drop your for loop iterating over ALL widgetIds and just use
void AppWidgetManager.updateAppWidget(ComponentName provider, RemoteViews views)
to update all your views at once.

Related

Android: How to update widget text on creation

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.

Update ListView Widget with Application

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.

Can't update AppWidget RemoteViews after re-enter the activity

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...

Android setTextViewText not working

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);
}
}

Change widget visibility on click

My widget consists two relative layouts. I have made both the layouts clickable. Following are the id's of the layout:
android:id="#+id/upper_layout"
android:id="#+id/bottom_layout"
Now, what I need is that if a user clicks on the upper_layout, bottom_layout should be invisible.
Here, is what I have tried so far but it's not working. Can u check what I am doing wrong?
Or, maybe suggest some other ways to achieve this.
Code :
public class BobsWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "Clicked";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
Intent active = new Intent(context, BobsWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.upper_layout,
actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// check, if our Action was called
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
remoteViews.setViewVisibility(R.id.bottom_layout, View.INVISIBLE);
}
super.onReceive(context, intent);
}
}
You have several inbuilt widget features available in Anndroid 3.0 or more versions.Check this link
I think you forgot to update the widget.
Have you tried something like this ?
remoteViews.setViewVisibility(R.id.bottom_layout, View.INVISIBLE);
final ComponentName provider = new ComponentName(context, this.getClass());
appWidgetManager.updateAppWidget(provider, views);

Categories

Resources