android - 如何使用聊天应用程序中的最后一条消息更新小部件
问题描述
一旦有新消息添加到列表中,我想用最后的消息更新小部件。我仍然是 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;
}
}
}
结果..小部件仅显示我的电子邮件
解决方案
推荐阅读
- javascript - 如何在不停止 Python 函数的情况下向 JS 发送返回值
- node.js - Puppeteer:仅为主页域添加基本身份验证标头,不适用于第 3 方请求
- node.js - 为什么异步等待不等待 - nodejs
- tensorflow - 训练后在 Keras 中修改自定义层
- flutter - 颤动,切换小部件在 showDialog 中无法正常工作
- django - 自定义用户/密码不正确时检索到的默认消息 djangorestframeworkssimple-jwt?
- function - 我需要知道的两个 Python 问题基础知识
- makefile - 难以将 $ORIGIN 添加到 LDFLAGS
- javascript - JavaScript 中奇怪的对象变量行为
- c++ - 如何在 constexpr 函数中使用 reverse_iterator