gpt4 book ai didi

一段时间后 Android 小部件按钮没有响应

转载 作者:太空狗 更新时间:2023-10-29 13:55:13 24 4
gpt4 key购买 nike

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

Widget

list :

<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_info:

<?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
.
.
}

关于一段时间后 Android 小部件按钮没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com