2

I Want to update my home screen widget label with data from firebase database for example my database contain 2 child messages and user i want to update widget with last message from user amr This is my code

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);
    }
}

messages represent the child in firebase database how can i do that ? please for bad english

Amr
  • 57
  • 1
  • 10
  • You are returing an emprty string as result of this `getInformation()` method, why? – Alex Mamo Jul 12 '18 at 13:13
  • I know .. so how to pass listview from another activity or from firebase to widget ? – Amr Jul 12 '18 at 16:56
  • I recommend you see the last part of my anwser from this **[post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** and also take a look at this **[video](https://www.youtube.com/watch?v=OvDZVV5CbQg)**. – Alex Mamo Jul 12 '18 at 17:10
  • @AlexMamo i checked your answer in the other post, but it doesn't work for me.. i did create an interface that works as a callback, but even so it doesn't update the views in the widget when the callback is invoked – Oscar Reyes Jun 28 '20 at 08:34
  • @OscarReyes In that case, please post a new question using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Jun 28 '20 at 19:37
  • @AlexMamo i did make a question and you can find it [here](https://stackoverflow.com/questions/62620884/android-how-to-show-data-in-widget-coming-from-firestore). for now it just shows how i am fetching the data but not how it is blocking or making the update to wait for the data – Oscar Reyes Jun 29 '20 at 13:02

0 Answers0