更新显示不同布局的应用小部件

时间:2018-07-19 10:45:38

标签: java android networking android-appwidget appwidgetprovider

在我的应用程序中,用户可以使用不同的帐户登录,而我想基于该帐户显示不同的数据,但是还存在一种状态,即用户尚未登录任何帐户。所以我想显示两种不同的布局。第一个包含帐户数据,第二个显示我的登录活动的按钮。我尝试切换按钮的可见性,但是当用户登录小部件时不会更新。

我的小部件的

xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/updating_chart_widget_iot"
    android:initialLayout="@layout/updating_chart_widget_iot"
    android:minHeight="250dp"
    android:minWidth="250dp"
    android:previewImage="@drawable/chart_widget_preview"
    android:resizeMode="vertical"
    android:updatePeriodMillis="60000"
    android:widgetCategory="home_screen">
</appwidget-provider>

这是我的窗口小部件提供程序的代码: 请告诉我我正在做哪一部分...

public class IoTUpdatingChartWidget extends AppWidgetProvider {
//    private PendingIntent serviceIntent;

    SharedPrefManager sharedPrefManager;
    ApiInterface apiService;

    ArrayList<String> hoursArrayList;
    ArrayList<Double> usagesArrayList;

    BarChart hourlyUsageChart;

    @Override
    public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        sharedPrefManager = SharedPrefManager.getInstance();
        apiService = ApiClient.getClient().create(ApiInterface.class);

        if (sharedPrefManager.isLoggedIn(context)) {

            Call<WidgetInfo> getWidgetInfoCall = apiService.getWidgetInfo(sharedPrefManager.getApiToken(context.getApplicationContext()));
            getWidgetInfoCall.enqueue(new Callback<WidgetInfo>() {
                @Override
                public void onResponse(Call<WidgetInfo> call, Response<WidgetInfo> response) {
                    Log.e("response code service", response.code() + "");
                    if (response.code() == 200) {

                        WidgetInfo widgetInfo = response.body();

                        RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.updating_chart_widget_iot);
//                        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
                        Log.e("chart arraysize service", widgetInfo.getHourlyUsageArray().size() + "");

                        ArrayList<HourlyUsage> hourlyUsageArrayList = widgetInfo.getHourlyUsageArray();

                        hoursArrayList = null;
                        usagesArrayList = null;
                        usagesArrayList = new ArrayList<>();
                        hoursArrayList = new ArrayList<>();

                        for (int i = 0; i < hourlyUsageArrayList.size(); i++) {
                            hoursArrayList.add(Utils.convertEng2FaNumbers(hourlyUsageArrayList.get(i).getHour()));
                            usagesArrayList.add(hourlyUsageArrayList.get(i).getEnergy());
                        }

                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        hourlyUsageChart = (BarChart) inflater.inflate(R.layout.bar_chart, null, false).findViewById(R.id.hourly_usage_chart);

                        initializeHourlyUsageChart(context);

                        Bitmap bitmap = Utils.takeScreenShotOfView(hourlyUsageChart, 350, 300);

                        view.setImageViewBitmap(R.id.image_view_chart, bitmap);

                        Drawable statusDrawable;
                        switch (Utils.getUsageTime()) {
                            case "پر باری":
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_red_36);
                                break;
                            case "میان باری":
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_yellow_36);
                                break;
                            case "کم باری":
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_green_36);
                                break;
                            default:
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_white_36);
                        }

                        view.setImageViewBitmap(R.id.image_view_network_status, Utils.drawableToBitmap(statusDrawable));
                        view.setTextViewText(R.id.text_view_usage_time, Utils.getUsageTime());
                        view.setTextViewText(R.id.text_view_cost, Utils.convertEng2FaNumbers(((int) widgetInfo.getCost() / 10) + " تومان"));
                        view.setTextViewText(R.id.text_view_user_score, Utils.convertEng2FaNumbers((int) widgetInfo.getRankInUsage() + " از " + (int) widgetInfo.getTotalUsers()));
                        view.setTextViewText(R.id.text_view_monthly_usage, Utils.convertEng2FaNumbers(((double)((int)(widgetInfo.getMonthlyUsage() *10 ))) / 10+ " kwh"));
                        view.setTextViewText(R.id.text_view_daily_usage, Utils.convertEng2FaNumbers(((double)((int)(widgetInfo.getDailyUsage() * 10))) / 10  + " kwh"));
                        view.setViewVisibility(R.id.progress_bar, View.GONE);
                        view.setViewVisibility(R.id.text_view_error, View.GONE);
                        view.setViewVisibility(R.id.button_enter, View.GONE);

                        ComponentName theWidget = new ComponentName(context, IoTUpdatingChartWidget.class);

                        AppWidgetManager manager = AppWidgetManager.getInstance(context);
                        manager.updateAppWidget(theWidget, view);

                    } else

                    {

                        //server error
                        RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.updating_chart_widget_iot);

                        Drawable statusDrawable;
                        switch (Utils.getUsageTime()) {
                            case "پر باری":
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_red_36);
                                break;
                            case "میان باری":
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_yellow_36);
                                break;
                            case "کم باری":
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_green_36);
                                break;
                            default:
                                statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_white_36);
                        }

                        view.setImageViewBitmap(R.id.image_view_network_status, Utils.drawableToBitmap(statusDrawable));
                        view.setTextViewText(R.id.text_view_usage_time, Utils.getUsageTime());

                        view.setTextViewText(R.id.text_view_cost, context.getString(R.string.short_no_data));
                        view.setTextViewText(R.id.text_view_user_score, context.getString(R.string.short_no_data));
                        view.setTextViewText(R.id.text_view_monthly_usage, context.getString(R.string.short_no_data));
                        view.setViewVisibility(R.id.progress_bar, View.VISIBLE);
                        view.setViewVisibility(R.id.text_view_error, View.VISIBLE);
                        view.setViewVisibility(R.id.image_view_chart, View.GONE);
                        view.setViewVisibility(R.id.button_enter, View.GONE);

                        view.setTextViewText(R.id.text_view_error, context.getString(R.string.server_err));
                        ComponentName theWidget = new ComponentName(context, IoTUpdatingChartWidget.class);

                        AppWidgetManager manager = AppWidgetManager.getInstance(context);
                        manager.updateAppWidget(theWidget, view);
                    }

                }

                @Override
                public void onFailure(Call<WidgetInfo> call, Throwable t) {
                    // internet error
                    if (hoursArrayList == null) {
                        RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.updating_list_widget_iot);

                        view.setViewVisibility(R.id.progress_bar, View.VISIBLE);
                        view.setViewVisibility(R.id.text_view_error, View.VISIBLE);
                        view.setViewVisibility(R.id.button_enter, View.GONE);
                        view.setViewVisibility(R.id.list_view, View.GONE);

                        view.setTextViewText(R.id.text_view_error, context.getString(R.string.internet_err));
                        ComponentName theWidget = new ComponentName(context, IoTUpdatingChartWidget.class);

                        AppWidgetManager manager = AppWidgetManager.getInstance(context);
                        manager.updateAppWidget(theWidget, view);
                    }
                    Log.e("chart responsecode serv", t.getMessage());
                }
            });

        } else if (!sharedPrefManager.isLoggedIn(context)) {

            // not logged in state
            RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.updating_list_widget_iot);

            view.setViewVisibility(R.id.progress_bar, View.VISIBLE);
            view.setViewVisibility(R.id.text_view_error, View.VISIBLE);
            view.setTextViewText(R.id.text_view_error, context.getString(R.string.login_to_your_account));
            view.setViewVisibility(R.id.button_enter, View.VISIBLE);
            view.setViewVisibility(R.id.progress_bar, View.GONE);

            view.setViewVisibility(R.id.list_view, View.GONE);

            Intent intent = new Intent(context, SplashScreenActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            view.setOnClickPendingIntent(R.id.button_enter, pendingIntent);

            Drawable statusDrawable;
            switch (Utils.getUsageTime()) {
                case "پر باری":
                    statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_red_36);
                    break;
                case "میان باری":
                    statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_yellow_36);
                    break;
                case "کم باری":
                    statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_green_36);
                    break;
                default:
                    statusDrawable = context.getResources().getDrawable(R.drawable.circle_filled_white_36);
            }

            view.setImageViewBitmap(R.id.image_view_network_status, Utils.drawableToBitmap(statusDrawable));
            view.setTextViewText(R.id.text_view_usage_time, Utils.getUsageTime());

            ComponentName theWidget = new ComponentName(context, IoTUpdatingChartWidget.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(context);
            manager.updateAppWidget(theWidget, view);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以强制主屏幕小部件的所有实例刷新如下:

{{1}}

我看不到登录和注销代码,但是可能只是在会话状态更改时调用updateWidgets()的情况。