Почему мой метод updateAppWidget не реагирует на все действия? - PullRequest
0 голосов
/ 10 июля 2019

Может кто-нибудь сказать мне, что не так с моим Broadcast приемником, поскольку он не реагирует на изменения времени?Он вызывает метод updateAppWidget, поскольку отображается сообщение Toast, но виджет часов не обновляет время.

Я перепробовал множество решений от 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 правильно вызывается из BroadcastReceiver.Могу ли я получить совет от кого-то умнее меня, пожалуйста?

1 Ответ

0 голосов
/ 22 июля 2019

ОК, я решил;)

Кажется, что я пытался назвать все экземпляры виджета как-то не так.После того как я добавил в метод onUpdate в своем классе AppWidgetProvider код:

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)

Все работают как во сне;)

...