gpt4 book ai didi

java - 为什么我的 updateAppWidget 方法不对所有操作使用react?

转载 作者:行者123 更新时间:2023-12-02 03:15:44 29 4
gpt4 key购买 nike

谁能告诉我我的广播接收器有什么问题,因为它对时间的变化没有反应?当我显示 Toast 消息时,它确实调用了 updateAppWidget 方法,但时钟小部件没有更新时间。

我已经尝试了来自 stackoverflow 的无数解决方案,包括为 TIME_TICK、TIME_CHANGED、TIME_ZONE_CHANGED 添加意图过滤器操作,但到目前为止我还没有运气。我知道我确实已经接近正确的解决方案,但我需要有人为我指明正确的方向。

这是我的 AppWidgetProvider 类代码:

package com.vulterey.nixieclockwidget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;


/**
* Implementation of App Widget functionality.
*/
public class NixieClock extends AppWidgetProvider {

BroadcastReceiver m_timeChangedReceiver = new Synchronize_Time();

public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {

int[] numberImage = new int[] {R.drawable.ne_zero,
R.drawable.ne_one, R.drawable.ne_two,
R.drawable.ne_three, R.drawable.ne_four,
R.drawable.ne_five, R.drawable.ne_six,
R.drawable.ne_seven, R.drawable.ne_eight,
R.drawable.ne_nine, R.drawable.ne_double_dot};

// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.nixie_clock);

// Get current time and format it with use of provided simple date format formatter.
Instant currentTime = Instant.now();
LocalDateTime localDateTime = LocalDateTime.ofInstant(currentTime, ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
String currentTimeToString = formatter.format(localDateTime);

// Get single digits from currentTime string and convert them into integers.
int TenthsOfHour = Integer.parseInt(currentTimeToString.substring(0,1));
int OnesOfHour = Integer.parseInt(currentTimeToString.substring(1,2));
int TenthsOfMinute = Integer.parseInt(currentTimeToString.substring(3,4));
int OnesOfMinute = Integer.parseInt(currentTimeToString.substring(4,5));

// Display current time by assign numberImage based on time integers to time views.
views.setImageViewResource(R.id.tenthsHourView, numberImage[TenthsOfHour]);
views.setImageViewResource(R.id.onesHourView, numberImage[OnesOfHour]);
views.setImageViewResource(R.id.doubleDotView, R.drawable.ne_double_dot);
views.setImageViewResource(R.id.tenthsSecondView, numberImage[TenthsOfMinute]);
views.setImageViewResource(R.id.onesSecondView, numberImage[OnesOfMinute]);

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);

Toast.makeText(context, "Broacast reciver works", Toast.LENGTH_LONG).show();
}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds)
updateAppWidget(context, appWidgetManager, appWidgetId);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}


@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created

// Create an intent filter
IntentFilter nixie_intentFilter = new IntentFilter();
nixie_intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
nixie_intentFilter.addAction(Intent.ACTION_TIME_TICK);
nixie_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
nixie_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);

context.getApplicationContext().registerReceiver(m_timeChangedReceiver, nixie_intentFilter);
}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
context.getApplicationContext().unregisterReceiver(m_timeChangedReceiver);
}
}

我的BroadcastReceiver类代码:

package com.vulterey.nixieclockwidget;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;


public class Synchronize_Time extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

Bundle extras = intent.getExtras();
if (extras != null) {
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
if (appWidgetId != -1) {

if ((action.equals(Intent.ACTION_TIME_TICK)) ||
(action.equals(Intent.ACTION_TIME_CHANGED)) ||
(action.equals(Intent.ACTION_TIMEZONE_CHANGED)) ||
(action.equals(Intent.ACTION_POWER_CONNECTED))) {

NixieClock.updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetId);


}

}
}
}
}

list 代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vulterey.nixieclockwidget">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".NixieClock">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.ACTION_TIMEZONE_CHANGED" />
<action android:name="android.appwidget.action.ACTION_TIME_CHANGED" />
<action android:name="android.appwidget.action.ACTION_TIME_TICK" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/nixie_clock_info" />
</receiver>
</application>

</manifest>

当小部件启动时,时间设置正确,显然 updateAppWidget 方法启动,但我通过更改充电/不充电状态从 BroadcastReceiver 类调用它,我收到 Toast 消息但保持不变,同时从逻辑上讲它应该更新为updateAppWidget 函数已从 BroadcastReceiver 正确调用。我可以从比我聪明的人那里得到一些建议吗?

最佳答案

好的,我解决了;)

似乎我试图以错误的方式调用小部件的所有实例。在我的 AppWidgetProvider 类中添加 onUpdate 方法后,代码:

ComponentName thisWidget = new ComponentName(context,
NixieClock.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

并在 BroadcastReceiver 类代码中更改为:

        Bundle extras = intent.getExtras();
if (extras != null) {
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
if (appWidgetId != -1)

至:

        ComponentName thisWidget = new ComponentName(context,
NixieClock.class);
int[] allWidgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(thisWidget);
for (int appWidgetId : allWidgetIds)

一切工作都像一场梦;)

关于java - 为什么我的 updateAppWidget 方法不对所有操作使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963114/

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