嗨,我在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);
但是,现在我试图将安装正常的文本插入,例如 char sequence contentTitle ="我的通知"; 在我的情况下显示一个变量。
我已经在打开一个广播电台时已经显示了此徽标图像,url,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";
向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);
}
});
}
带有默认通知布局,您将获得两行:标题和文本。如果您想要更多的灵活性,则必须创建一个自定义布局。
而不是设置标题和文本,请实例化 RemoteViews
,
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom);
,然后将其设置为您的通知对象
mNotification.contentView = contentView;
RemoteViews
的工作方式与普通视图有所不同。您必须调用RemoteViews
对象上的方法以将值设置为内部的视图。例如,
contentView.setTextViewText(R.id.text_view_id, "Hello, world.");
您可以看到,可以在布局中使用的视图类型,并且可以对其进行调用的方法受到限制。