我想在将新消息添加到列表中后,用最新消息更新小部件。 我仍然是android的初学者,我当然学习了这个笔记 但我不能使用它
注意:
回想一下AppWidgetProvider是BroadcastReceiver,所以它 侦听使用sendBroadcast()方法发送的消息。从而, 您应该确定制作新邮件的方法,并且 将它们保存在数据库中,然后您可以让小部件知道 阅读新邮件。然后,一旦小部件知道有更新 (由于您的广播消息),它可以更新消息列表 使用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;
}
}
}
结果..小部件仅显示我的电子邮件