首页 > 解决方案 > 如何使用聊天应用程序中的最后一条消息更新小部件

问题描述

一旦有新消息添加到列表中,我想用最后的消息更新小部件。我仍然是 android 的初学者,我学习过的课程中的这个笔记,但我不能使用它会

笔记:

回想一下,AppWidgetProvider 是一个 BroadcastReceiver,所以它监听使用 sendBroadcast() 方法发送的消息。因此,您应该确定您编写新消息的方法,并将它们保存在数据库中,并且您可以让 Widget 知道有新消息要阅读。然后,一旦 Widget 知道有更新(感谢您的广播消息),它就可以通过使用 onDataSetChanged() 方法(在 RemoteViewsFactory 类中声明)从数据库中获取数据来更新消息列表。

这是我的代码

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.new_app_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.widget_title, 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.new_app_widget);
            views.setTextViewText(R.id.information, 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("users");
        databaseReference.orderByChild("email").equalTo("Yasmeen.angel2017@gmail.com").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                try {
                    MessageModel m = dataSnapshot.getChildren().iterator().next().getValue(MessageModel.class);
                    Log.d("user:", m.getName());
                    views.setTextViewText(R.id.admin, m.getName() + '\n' + m.getName());
                } catch (Throwable b) {

                }

            }

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

    public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> {


        private RemoteViews views;

        private String url = "http://findicons.com/files/icons/2101/ciceronian/59/photos.png";

        public DownloadBitmap(RemoteViews views) {
            this.views = views;
        }

        @Override
        protected Bitmap doInBackground(String... params) {

            try {
                InputStream in = new java.net.URL(url).openStream();
                Bitmap bitmap = BitmapFactory.decodeStream(in);
                return bitmap;

            } catch (Exception e) {
                Log.e("ImageDownload", "Download failed: " + e.getMessage());
            }
            return null;
        }

    }
}

结果..小部件仅显示我的电子邮件

标签: android

解决方案


推荐阅读