我使用android开发了聊天应用程序,现在可以正常工作了,我想在主屏幕小部件中显示最新消息,但不显示任何相关信息。我希望用户可以在此小部件中看到最新消息,我该怎么做? 这是我的代码
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("Yasmeenahmed@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);}
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;
} }}
和我的xml
<FrameLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/widget_layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaDEDFDE"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:scaleType="fitXY" />
<TextView
android:id="@+id/widget_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/colorPrimary"
android:gravity="center"
android:paddingEnd="@dimen/widget_header_padding"
android:paddingLeft="@dimen/widget_header_padding"
android:paddingRight="@dimen/widget_header_padding"
android:paddingStart="@dimen/widget_header_padding"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/primary_light"
android:gravity="center"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/admin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:gravity="center"
android:minLines="3"
android:padding="4dp"
android:text="@string/admin_information"
android:textColor="@android:color/black"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
出什么问题了,对不起,蝙蝠英语?