Appwidget keeps reseting background to transparent

拥有回忆 提交于 2019-12-13 05:38:59

问题


I created functionality in an appwidget that allows a user to change the background from transparent to semi-transparent. The default is set to transparent in the xml. When the user changes the background preference the setting is saved and the background is updated using below code:

public static Bitmap setBackground (int bgcolor)
{
  {
    Bitmap.Config config=Bitmap.Config.ARGB_8888; 
    Bitmap bitmap=Bitmap.createBitmap(2, 2, config);
    Canvas canvas=new Canvas(bitmap); 
    canvas.drawColor(bgcolor);
    return bitmap;
  }
}

/* set background */
if (ExampleWidgetProvider.background==1)
  views.setImageViewBitmap(R.id.bgcolor, setBackground(Color.parseColor("#aaaaaaaa")));
else
  views.setImageViewBitmap(R.id.bgcolor, setBackground(Color.parseColor("#00000000")));

One user reports that the background is changed back to transparent when he has set it to be semi-transparent. This may happen seemingly random within a few minutes an hour or half a day.

I realised this is a problem with the background and not some kind of preference resetting because I sent the user a version of the appwidget that will always change the background to semi-transparent, i.e. using:

/* set background */
if (ExampleWidgetProvider.background==1)
  views.setImageViewBitmap(R.id.bgcolor, setBackground(Color.parseColor("#aaaaaaaa")));
else
  views.setImageViewBitmap(R.id.bgcolor, setBackground(Color.parseColor("#aaaaaaaa")));

But interestingly that didn't fix it. Since the default in the xml is set to transparent I am suspecting the app somehow is either restarted or redrawn and the default layout is being used.

How can I detect and/or work around this?

Update: User (as expected) confirmed fix which permanently changes background colour in xml to semi transparent is working. Which means the system is resetting the background for some unknown reason.

The user uses an LG PG 970 V. 2.2.2 and for what it's worth he is using "Juice Defender Ultimate" and has it configured to not connect to the internet during the night. My appwidget does connect to the internet at regular intervals.


回答1:


Well, I actually found a way to reproduce it. I have an LG with a slide out keyboard, which rotates the home screen when you slide out the keyboard. Once it does that the semi transparent background disappears. It looks like I have to detect some kind of redraw event.

I found a work around, or solution, not entirely sure. Either way it solves the problem, at least when I test it with the above mentioned method of reproducing.

I implement a service that I keep running permanently, which overrides onConfigurationChanged() method.

For ideas see:

  • Button click lost on widget when screen is rotated
  • http://developer.android.com/reference/android/content/Intent.html#ACTION_CONFIGURATION_CHANGED

you can not receive this through components declared in manifests

This provides solutions for activities, but I am not sure yet how to use something like that in appwidgets, if at all possible:

  • Saving Android Activity state using Save Instance State

Code sample, this service is started in the AppWidgetProvider class in the onEnabled() method. So it's only run once regardless of how many instances of your appwidget are running and should ideally be able to deal with any instance.

public class ExampleConfigChangeService extends Service
{
  @Override
  public void onCreate()
  {
    super.onCreate();
    /* whatever code you need here */
  }

@Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); /* whatever code you need here */ }

/* you need this or it will not compile **/ @Override public IBinder onBind(Intent intent) { /* We don't need to bind to this service */ return null; }

@Override public void onConfigurationChanged(Configuration newConfig) { /* * this is the code that will run whenever an ACTION_CONFIGURATION_CHANGED happens * do whatever needs to be done, such as re-reading preferences, * resetting background to user's preferred setting etc. */ } }



来源:https://stackoverflow.com/questions/9624940/appwidget-keeps-reseting-background-to-transparent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!