Android的自定义通知布局RemoteViews [英] Android custom notification layout with RemoteViews

查看:301
本文介绍了Android的自定义通知布局RemoteViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用此帖子我的Andr​​oid应用自定义通知我偶然,我试图解决的最后2小时的问题。

I'm trying to create a custom notification for my android app using this post, and I stumbled upon a problem which I tried to resolve for the last 2 hours.

我的布局中只有ImageView的显示,我无法弄清楚如何使它像预期。

Only the imageview of my layout shows, and I can't figure out how to make it works like intended.

我的code:

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文件:

The xml file:

<?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>

我的通知

感谢您的帮助。

推荐答案

问题是你有没有设置正确的内容查看。

Problem is you have not set the correct contentView.

mBuilder.setContent(contentView);

但你的远程视图是mContentView。将其更改为

But your remote view is mContentView. change it to

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());

这篇关于Android的自定义通知布局RemoteViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆