Пользовательский макет уведомлений Android с RemoteViews
Я пытаюсь создать собственное уведомление для моего приложения Android, используя этот пост, и я наткнулся на проблему, которую я пытался решить за последние 2 часа.
Показывается только изображение моего макета, и я не могу понять, как заставить его работать, как предполагалось.
Мой код:
package be.ac.ucl.lfsab1509.cumulus.services;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.RemoteViews;
import be.ac.ucl.lfsab1509.cumulus.R;
import be.ac.ucl.lfsab1509.cumulus.activities.MainActivity;
import be.ac.ucl.lfsab1509.cumulus.entities.Song;
public class CumulusNotification {
private CumulusService mCtx;
private CumulusMediaPlayer mPlayer;
private Notification mNotification;
private NotificationCompat.Builder mBuilder;
private RemoteViews mContentView;
public CumulusNotification(CumulusService ctx, CumulusMediaPlayer player){
mCtx = ctx;
mPlayer = player;
mBuilder = new NotificationCompat.Builder(
mCtx);
mBuilder.setSmallIcon(R.drawable.ic_stat_cumulus_notification);
//mBuilder.setContentTitle("Cumulus is playing");
//mBuilder.setTicker("Cumulus");
//mBuilder.setContentText(mCtx.getCurrentSong().title());
Intent resultIntent = new Intent(mCtx, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
resultIntent.setAction(Intent.ACTION_MAIN);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mCtx);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mContentView = new RemoteViews(mCtx.getPackageName(), R.layout.statusbar);
mContentView.setImageViewResource(R.id.notifimage, R.drawable.cumulus_icon);
mContentView.setTextViewText(R.id.notiftitle, "Custom notification");
mContentView.setTextViewText(R.id.notiftext, "This is a custom layout");
mBuilder.setContent(mContentView);
}
public void startNotification() {
mNotification = mBuilder.build();
//mNotification.contentView = mContentView;
mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}
public void updateSong(Song song){
mBuilder.setContentText(song.title());
mNotification = mBuilder.build();
mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}
}
Файл xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:gravity="left" >
<ImageView android:id="@+id/notifimage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/notiftitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notifimage" />
<TextView android:id="@+id/notiftext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notifimage"
android:layout_below="@id/notiftitle" />
</RelativeLayout>
![my notification]()
Спасибо за помощь
Ответы
Ответ 1
Проблема в том, что вы не установили правильный контент.
mBuilder.setContent(contentView);
Но ваш удаленный просмотр - mContentView. измените его на
mBuilder.setContent(mContentView);
Вот пример
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher);
RemoteViews mContentView = new RemoteViews(getPackageName(), R.layout.test_tt);
mContentView.setImageViewResource(R.id.image, R.drawable.ic_action_search);
mContentView.setTextViewText(R.id.title, "Custom notification");
mContentView.setTextViewText(R.id.text, "This is a custom layout");
mBuilder.setContent(mContentView);
startForeground(1001, mBuilder.build());
Ответ 2
Здесь вы можете увидеть рабочий пример:
http://www.laurivan.com/android-notifications-with-custom-layout/
Я думаю, добавив
// Set Icon
.setSmallIcon(R.drawable.smallimage)
// Set Ticker Message
.setTicker("Noification is created");
вашему строителю может решить проблему.
Ответ 3
Я думаю, что это всего лишь макет, тем больше я думаю об этом. Я думаю, что если бы вы установили android:layout_alignParentRight="true"
на TextView
s, вы получили бы то, что искали. Похоже, что он выкладывает их справа от ImageView
, но не дает им никакой ширины во время компоновки, так как они не просят ни одного, и у них нет никакого контента.
Если это не сработает, сообщите нам!
Ответ 4
Это сообщение очень круто.
В основном и так:
RemoteViews downloadViews = new RemoteViews(context.getPackageName(), R.layout.download_notification);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setContent(downloadViews)
.setPriority(NotificationCompat.PRIORITY_MIN);
final Notification notification = mBuilder.build();
notification.bigContentView = downloadViews;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);