问题描述
我在appwidget允许用户以改变从透明到半透明后台创建功能。默认设置为透明的XML。当用户更改背景preference设置将被保存,并在后台更新使用下面code:
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.
我意识到这是背景的问题,而不是某种preference复位,因为我发送的用户一个版本appwidget的,将永远改变背景使用半透明的,即:
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")));
但有趣的是这并没有解决它。
由于在XML中的默认设置为透明,我怀疑应用程序以某种方式要么是重新启动或重新绘制和正在使用的默认布局。
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?
更新:用户(如预期)证实修复它将永久更改背景颜色在XML中半透明的工作。这意味着系统正在重置一些未知的原因的背景。
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.
用户使用的LG PG 970 V. 2.2.2和它的价值,他是用果汁后卫终极,并有它配置在夜间无法连接到互联网。我appwidget并连接到定期的互联网连接。
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.
推荐答案
好吧,我居然找到了一种方法来复制它。我有一个LG带有滑出式键盘,当你滑出键盘进行旋转的主屏幕。一旦它的半透明背景消失。它看起来像我有检测某种重绘事件的。
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.
我实现了我一直永久运行的服务,它覆盖onConfigurationChanged()方法。
I implement a service that I keep running permanently, which overrides onConfigurationChanged() method.
有关的想法,请参阅:
- Button click lost on widget when screen is rotated
- http://developer.android.com/reference/android/content/Intent.html#ACTION_CONFIGURATION_CHANGED
您无法通过舱单申报组件收到此
这提供了活动方案,但我还不确定如何使用类似的东西在appwidgets,如果在所有可能的:
This provides solutions for activities, but I am not sure yet how to use something like that in appwidgets, if at all possible:
- How do I save an Android application's state?
code样本,该服务在onEnabled()方法AppWidgetProvider类开始。因此它只运行一次,无论你如何的appwidget许多情况下,正在运行的,因此最好能够应付任何情况。
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 */
}
@覆盖
公共无效调用onStart(意向意图,诠释startId)
{
super.onStart(意向,startId);
/ *您在这里需要什么code * /
}
@Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); /* whatever code you need here */ }
/ *你需要这个,否则将不能编译*的 /
@覆盖
公众的IBinder onBind(意向意图)
{
/ 的我们并不需要绑定到该服务* /
返回null;
}
/* 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; }
@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG)
{
/ *
*这是code每当ACTION_CONFIGURATION_CHANGED情况,将运行
*做任何需要做的事情,例如重新阅读preferences,
*重设背景,以用户的preferred设置等。
* /
}
}
@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. */ }}
这篇关于Appwidget保持CH375复位背景透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!