如何使用Firebase数据更新小部件

时间:2018-07-11 21:46:30

标签: android firebase firebase-realtime-database android-widget

我想用Firebase数据库中的数据更新主屏幕小部件标签 例如我的数据库包含2条子消息,并且用户我想用用户amr的最后一条消息更新小部件 这是我的代码

NewAppWidget.java

public class NewAppWidget extends AppWidgetProvider {
    private static final String ACTION_BROADCASTWIIDGET = "ACTION_BROADCASTWIIDGET";
    RemoteViews views;

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


        //Construct the RemoteViews object
        views = new RemoteViews(context.getPackageName(), R.layout.messages_widget);
        Intent mainIntent = new Intent(context, MainActivity.class);
        PendingIntent mainPendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);
        new DownloadBitmap(views).execute(" TXTME chat");

        views.setOnClickPendingIntent(R.id.appwidget_text, mainPendingIntent);
        Intent intent = new Intent(context, NewAppWidget.class);
        intent.setAction(ACTION_BROADCASTWIIDGET);

        context.sendBroadcast(intent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (ACTION_BROADCASTWIIDGET.equals(intent.getAction())) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.messages_widget);
            views.setTextViewText(R.id.appwidget_text, getInformation());
            ComponentName appWidget = new ComponentName(context, NewAppWidget.class);
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            appWidgetManager.updateAppWidget(appWidget, views);
        }
    }

    private String getInformation() {

        DatabaseReference databaseReference =
                FirebaseDatabase.getInstance().getReference().child("messages");
        databaseReference.orderByChild("user").equalTo("amrkamal2").addListenerForSingleValueEvent
                (new ValueEventListener() {
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    try {
                        MessageModel m = dataSnapshot.getChildren().iterator().next().getValue(MessageModel.class);
                        if (m != null) {
                            views.setTextViewText(R.id.appwidget_text, m.getText());
                        }

                    } catch (Throwable ignored) {}
                } }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.w("", "load message:onCancelled");
            }
        });
        return "";
    }

    @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) {
        super.onEnabled(context);
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }
}

消息表示Firebase数据库中的子级 我怎样才能做到这一点 ?请为英语不好

0 个答案:

没有答案