我创建了一个转换单位的小部件。问题是,几个小时后,小部件上的按钮不再响应。检查图像和下面的代码:
android - 一段时间后,Android Widget按钮不响应-LMLPHP
显示:

<receiver
            android:name=".provider.RechnerWidgetProvider"
            android:label="@string/widget_label_rechner">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.xxx.xxxxx.WIDGET_BUTTON" />               <!--Packagename-->
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/rechner_appwidget_info" />
</receiver>

XML/Rechner_AppWidget_信息:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="250dp"
    android:minHeight="40dp"
    android:initialLayout="@layout/widget_rechner"
    android:previewImage="@drawable/widget_rechner_preview"
    android:updatePeriodMillis="3600000"
    android:resizeMode="horizontal">

</appwidget-provider>

RechnerWidgetProvider:提供程序
public class RechnerWidgetProvider extends AppWidgetProvider{

    private final String UNIT_ACTION = ".WIDGET_BUTTON"; //Receiver Action in der Manifest

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_rechner);

        //Open app button
        remoteViews.setOnClickPendingIntent(R.id.calc, PendingIntent.getActivity(context, 0, new Intent(context, RechnerActivity.class), 0));

        //Switch Units button
        remoteViews.setOnClickPendingIntent(R.id.unit, PendingIntent.getBroadcast(context, 0, new Intent(context.getPackageName() + UNIT_ACTION), PendingIntent.FLAG_UPDATE_CURRENT));

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_rechner);

        if(intent.getAction().equals(context.getPackageName() + UNIT_ACTION)){

            SharedPreferences data = context.getSharedPreferences(CustomAdapterFilter.FILENAME_SETTINGS, Context.MODE_PRIVATE);
            int counter = data.getInt("counter", 0);

            switch (counter){
                case 0: //Zeige 1lbs nach kg
                    remoteViews.setTextViewText(R.id.unit, context.getString(R.string.lb));
                    remoteViews.setTextViewText(R.id.value, "= 0.4536 "     + context.getString(R.string.kg));
                    counter++;
                    break;
                case 1: //Zeige 1cm nach inches
                    remoteViews.setTextViewText(R.id.unit, context.getString(R.string.cm));
                    remoteViews.setTextViewText(R.id.value, "= 0.3937 "     + context.getString(R.string.inches));
                    counter++;
                    break;
                case 2: //Zeige 1inch nach cm
                    remoteViews.setTextViewText(R.id.unit, context.getString(R.string.inch));
                    remoteViews.setTextViewText(R.id.value, "= 2.54 "       + context.getString(R.string.cm));
                    counter++;
                    break;
                case 3: //Zeige 1kg nach lbs
                    remoteViews.setTextViewText(R.id.unit, context.getString(R.string.kg));
                    remoteViews.setTextViewText(R.id.value, "= 2.2046 "     + context.getString(R.string.lbs));
                    counter = 0;
                    break;
            }

            SharedPreferences.Editor editor = data.edit();
            editor.putInt("counter", counter);
            editor.apply();

            updateWidget(context, remoteViews);
        }

        super.onReceive(context, intent);
    }

    public void updateWidget(Context context, RemoteViews remoteViews){
        ComponentName widgetComponent = new ComponentName(context, RechnerWidgetProvider.class);
        AppWidgetManager.getInstance(context).updateAppWidget(widgetComponent, remoteViews);
    }

}

最佳答案

阅读此answer了解击球手
onReceive方法中添加此行

remoteViews.setOnClickPendingIntent(R.id.unit, PendingIntent.getBroadcast(context, 0, new Intent(context.getPackageName() + UNIT_ACTION), PendingIntent.FLAG_UPDATE_CURRENT));

当启动程序重新启动时,您的AppWidgetProvider停止更新onUpdate未调用,这就是为什么remoteViews.setOnClickPendingIntent未启动单击
 @Override
    public void onReceive(Context context, Intent intent) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_rechner);
        remoteViews.setOnClickPendingIntent(R.id.unit, PendingIntent.getBroadcast(context, 0, new Intent(context.getPackageName() + UNIT_ACTION), PendingIntent.FLAG_UPDATE_CURRENT));
         .
         .
         .
         .   your code
         .
         .
    }

07-24 21:13