Android - Samsung: Creating Widget with configuration activity fails

有些话、适合烂在心里 提交于 2019-12-02 10:43:15

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/40708973/android-samsung-creating-widget-with-configuration-activity-fails

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