Android - Samsung: Creating Widget with configuration activity fails(Android - 三星:使用配置活动创建小部件失败)
问题描述
我构建了一个可以将小部件添加到主屏幕的应用程序.该小部件适用于我的 Nexus 6P 和摩托罗拉 Moto G3.
使用三星手机(使用 S3 mini (4.1.2)、S5、S6 (6.0.1) 测试)根本不添加小部件或 TouchWiz 崩溃.
使用另一个启动器 (Nova),小部件也不会在 S3 mini 上创建.
在 logcat 中我根本看不到任何错误消息.
我试图尽可能缩小问题的范围.如果我从 counter_widget_info.xml 中删除 android:configure="de.cliff.strichliste.widgets.WidgetConfigurationActivity"
,则会创建小部件.如果我想使用配置活动 TouchWiz 在三星 S3 mini 上崩溃.
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"安卓:minWidth="40dp"安卓:minHeight="40dp"android:configure="de.cliff.strichliste.widgets.WidgetConfigurationActivity"安卓:updatePeriodMillis="1800000"android:initialLayout="@layout/widget_counter"android:resizeMode="水平|垂直"android:widgetCategory="home_screen"android:previewImage="@drawable/widget_preview"></appwidget-provider>
在 AndroidManifest.xml 中,我使用以下行注册小部件:
在 Java 方面,我在 WidgetConfigurationActivity 中有以下内容:
公共类 WidgetConfigurationActivity 扩展 Activity {@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);WidgetConfigurationBinding 绑定 = DataBindingUtil.setContentView(this, R.layout.widget_configuration);设置结果(结果确定);}}
这在 WidgetProvider 类中:
公共类 CounterWidgetProvider 扩展 AppWidgetProvider {@覆盖公共无效 onUpdate(上下文上下文,AppWidgetManager appWidgetManager,int [] appWidgetIds){super.onUpdate(context, appWidgetManager, appWidgetIds);Intent intent = new Intent(context, CounterWidgetProvider.class);context.startService(intent);}}
在我自己的三星设备上进行测试,如果结果 Intent
没有应用程序,似乎问题是 TouchWiz 窒息附加的小部件 ID,即使它是 RESULT_CANCELLED
.
来自App Widget 开发者页面:p><块引用>
- App Widget 宿主调用配置 Activity,配置 Activity 应始终返回结果.结果应包括启动 Activity 的 Intent 传递的 App Widget ID(保存在 Intent extras 中为 EXTRA_APPWIDGET_ID).
目前尚不清楚您最终希望如何处理结果,但在 onCreate()
中设置有效结果绝对是个好主意,以防用户退出配置活动
.例如:
final int id = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);最终意图结果 = new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);setResult(RESULT_OK, 结果);
I built an application with the possibility to add widgets to the home screen. The widget is working with my Nexus 6P and a Motorola Moto G3.
With Samsung Phones (Tested with S3 mini (4.1.2), S5, S6 (6.0.1)) the widget is not added at all or TouchWiz crashes.
With another launcher (Nova) the widget is also not created on the S3 mini.
In logcat I don't see any error message at all.
I tried to narrow the problem down as far as possible.
The widget gets created if I remove the android:configure="de.cliff.strichliste.widgets.WidgetConfigurationActivity"
from the counter_widget_info.xml. If I want to use a configuration activity TouchWiz crashes on the Samsung S3 mini.
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:configure="de.cliff.strichliste.widgets.WidgetConfigurationActivity"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget_counter"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
android:previewImage="@drawable/widget_preview">
</appwidget-provider>
In the AndroidManifest.xml I register the widget with the following lines:
<receiver
android:name=".widgets.CounterWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/counter_widget_info" />
</receiver>
On the Java side, I've got the following in the WidgetConfigurationActivity:
public class WidgetConfigurationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WidgetConfigurationBinding binding = DataBindingUtil.setContentView(this, R.layout.widget_configuration);
setResult(RESULT_OK);
}
}
And this in the WidgetProvider class:
public class CounterWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent intent = new Intent(context, CounterWidgetProvider.class);
context.startService(intent);
}
}
From testing on my own Samsung devices, it seems the problem is that TouchWiz chokes if the result Intent
doesn't have the App Widget ID attached, even if it's RESULT_CANCELLED
.
From the App Widget developer page:
- The App Widget host calls the configuration Activity and the configuration Activity should always return a result. The result should include the App Widget ID passed by the Intent that launched the Activity (saved in the Intent extras as EXTRA_APPWIDGET_ID).
It's unclear how exactly you wish to ultimately handle the result, but it's definitely a good idea to go ahead and set a valid result in onCreate()
, in case the user backs out of the configuration Activity
. For example:
final int id = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
final Intent result = new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
setResult(RESULT_OK, result);
这篇关于Android - 三星:使用配置活动创建小部件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android - 三星:使用配置活动创建小部件失败


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01