谁能告诉我我的广播接收器出了什么问题,因为它对时间的变化没有反应? 当我显示Toast消息时,它确实调用了updateAppWidget方法,但Clock小部件未更新时间。
我已经尝试过无数种来自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);
}
}
}
}
}
清单代码:
<?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函数。我可以从比我聪明的人那里得到一些建议吗?
答案 0 :(得分:0)
好的,我解决了;)
似乎我试图以某种错误的方式调用小部件的所有实例。 在我的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)
所有工作都像一场梦;)