为什么我的 updateAppWidget 方法对所有操作都没有反应?
Why my updateAppWidget method doesn't react for all actions?
谁能告诉我我的广播接收器有什么问题,因为它对时间的变化没有反应?
它确实调用了 updateAppWidget 方法,因为我正在显示 Toast 消息,但时钟小部件不更新时间。
我已经尝试了无数来自 Whosebug 的解决方案,包括为 TIME_TICK、TIME_CHANGED、TIME_ZONE_CHANGED 添加意图过滤器操作,但到目前为止我没有运气。我知道我真的接近一个合适的解决方案,但我需要有人为我指明正确的方向。
这是我的 AppWidgetProvider class 代码:
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 class 代码:
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);
}
}
}
}
}
以及清单代码:
<?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 方法启动了,但我通过更改 charging/not 充电状态从 BroadcastReceiver class 调用它我收到 Toast 消息但保持不变虽然从逻辑上讲,它应该随着从 BroadcastReceiver 正确调用 updateAppWidget 函数而更新。我可以从比我聪明的人那里得到一些建议吗?
好的,我解决了;)
似乎我试图以错误的方式调用小部件的所有实例。
在我的 AppWidgetProvider class 添加到 onUpdate 方法后,代码:
ComponentName thisWidget = new ComponentName(context,
NixieClock.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
并在 BroadcastReceiver class 代码中更改:
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)
一切如梦如幻;)
谁能告诉我我的广播接收器有什么问题,因为它对时间的变化没有反应? 它确实调用了 updateAppWidget 方法,因为我正在显示 Toast 消息,但时钟小部件不更新时间。
我已经尝试了无数来自 Whosebug 的解决方案,包括为 TIME_TICK、TIME_CHANGED、TIME_ZONE_CHANGED 添加意图过滤器操作,但到目前为止我没有运气。我知道我真的接近一个合适的解决方案,但我需要有人为我指明正确的方向。
这是我的 AppWidgetProvider class 代码:
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 class 代码:
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);
}
}
}
}
}
以及清单代码:
<?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 方法启动了,但我通过更改 charging/not 充电状态从 BroadcastReceiver class 调用它我收到 Toast 消息但保持不变虽然从逻辑上讲,它应该随着从 BroadcastReceiver 正确调用 updateAppWidget 函数而更新。我可以从比我聪明的人那里得到一些建议吗?
好的,我解决了;)
似乎我试图以错误的方式调用小部件的所有实例。 在我的 AppWidgetProvider class 添加到 onUpdate 方法后,代码:
ComponentName thisWidget = new ComponentName(context,
NixieClock.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
并在 BroadcastReceiver class 代码中更改:
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)
一切如梦如幻;)