Android - 三星:使用配置活动创建小部件失败 [英] Android - Samsung: Creating Widget with configuration activity fails

查看:33
本文介绍了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"安卓:m​​inWidth="40dp"安卓:m​​inHeight="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 - 三星:使用配置活动创建小部件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆