嗨,我正在 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);
}
});
}