您好我正在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 =“我的通知”; 一样在我的情况下显示一个变量来自流的shoutcast服务器的元数据。
我有这个已经显示当我打开一个广播电台时,它会显示来自shoutcast网址的电台,网址,标题的徽标图片,并带有以下代码:
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";
显示来自shoutcast的元数据?
非常感谢。
编辑:
完整代码:
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);
}
});
}
答案 0 :(得分:1)
使用默认通知布局,您会得到两行:标题和文本。如果你想要更灵活,你必须创建一个自定义布局。
代替设置标题和文字,实例化RemoteViews
,
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom);
然后将其设置为通知对象
mNotification.contentView = contentView;
RemoteViews
的工作方式与普通视图略有不同。你必须调用RemoteViews
对象上的方法来将值设置到其中的视图中。例如,
contentView.setTextViewText(R.id.text_view_id, "Hello, world.");
如您所见,您可以在布局中使用的视图类型以及可以在其上调用的方法都是有限的。