-1

嗨,我正在 android 中创建一个 aac 播放器应用程序,我已经有这样的通知管理器:

Context context = getApplicationContext();
String ns = Context.NOTIFICATION_SERVICE;
int icon = R.drawable.icon;
CharSequence tickerText = "Now playing...";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "My notification";
CharSequence contentText = "txtMetaTitle";
Intent notificationIntent = new Intent(AACPlayerActivity.this, Principal.class);
notification.flags = Notification.FLAG_ONGOING_EVENT;

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(1, notification);

但现在我试图插入普通文本,如CharSequence contentTitle = "My notification"; 而是在我的情况下显示一个变量,即来自流的广播服务器的元数据。

当我打开广播电台时,我已经有这个显示了,它显示了电台的徽标图像,url,来自shoutcast url的标题,带有以下代码:

public void playerMetadata( final String key, final String value ) {
    TextView tv = null;

    if ("StreamTitle".equals( key ) || "icy-name".equals( key ) || "icy-description".equals( key )) {
        tv = txtMetaTitle;
    }
    else if ("icy-url".equals( key )) {
        tv = txtMetaUrl;
    }
    else return;

    final TextView ftv = tv;

    uiHandler.post( new Runnable() {
        public void run() {
            ftv.setText( value );
        }
    });
}

我如何显示例如txtMetaTitle并在通知中实现它:

CharSequence contentTitle = "My notification";
CharSequence contentText = "txtMetaTitle";

显示来自直播的元数据?

非常感谢你。

编辑:

完整代码:

 private void start() {
        stop();

        txtMetaTitle.setText("");
        txtMetaGenre.setText("");
        txtMetaUrl.setText("");

        multiPlayer = new MultiPlayer( this, getInt( txtBufAudio ), getInt( txtBufDecode ));

        //multiPlayer.playAsync(getUrl());

        Log.d(TAG, "onClick: starting srvice");


        myIntent = new Intent(this, MyService.class);
        myIntent.putExtra("URL", getUrl());
        myIntent.putExtra("MP",new  Sharable(multiPlayer));
        startService(myIntent);
        Context context = getApplicationContext();
        String ns = Context.NOTIFICATION_SERVICE;
        int icon = R.drawable.icon;
        CharSequence tickerText = "Now playing...";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        CharSequence contentTitle = "My notification";
        CharSequence contentText = txtMetaTitle.getText();
        Intent notificationIntent = new Intent(AACPlayerActivity.this, Principal.class);
        notification.flags = Notification.FLAG_ONGOING_EVENT;

        PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
       NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
       mNotificationManager.notify(1, notification);
    }

我修复了在此内部移动通知服务:

 public void playerMetadata( final String key, final String value ) {
        TextView tv = null;

        if ("StreamTitle".equals( key ) || "icy-name".equals( key ) || "icy-description".equals( key )) {
            tv = txtMetaTitle;
        }
        else if ("icy-url".equals( key )) {
            tv = txtMetaUrl;
        }
        else return;

        final TextView ftv = tv;

        uiHandler.post( new Runnable() {
            public void run() {
                ftv.setText( value );
                System.out.println("Now playing..." + txtMetaTitle.getText().toString());
                Context context = getApplicationContext();
                String ns = Context.NOTIFICATION_SERVICE;
                int icon = R.drawable.icon;
                CharSequence tickerText = "Now playing...";
                long when = System.currentTimeMillis();
                Notification notification = new Notification(icon, tickerText, when);
                CharSequence contentTitle = txtMetaTitle.getText().toString();
                CharSequence contentText = txtMetaUrl.getText().toString();
                Intent notificationIntent = new Intent(AACPlayerActivity.this, Principal.class);
                notification.flags = Notification.FLAG_ONGOING_EVENT;
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

                //PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
               NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
               mNotificationManager.notify(1, notification);
            }
        });
    }
4

1 回答 1

1

使用默认通知布局,您会得到两行:标题和文本。如果您想要更多的灵活性,则必须创建自定义布局。

而不是设置标题和文本,而是实例化 a RemoteViews

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom);

然后将其设置到您的通知对象中,

mNotification.contentView = contentView;

RemoteViews工作方式与正常视图略有不同。您必须调用RemoteViews对象上的方法才能将值设置到其中的视图中。例如,

contentView.setTextViewText(R.id.text_view_id, "Hello, world.");

如您所见,您可以在布局中使用的视图类型以及您可以在它们上调用的方法是有限的。

于 2012-10-19T22:23:46.813 回答