我需要帮忙。我尝试在android状态栏中显示一个通知。
它将通知用户与服务器的同步正在进行中。
现在,我使用这段代码,它工作得很好: NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.stat_notify_sync).setWhen(System.currentTimeMillis())
.setAutoCancel(false).setOngoing(true);
NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.getNotification();
nm.notify(1, notif);
唯一的问题是当用户展开状态栏时创建了一个视图。我想做的是只显示一个小图标在状态栏的顶部,没有contentview。
有人知道怎么做吗?提前感谢!
这是不可能的
基本原理是这样的:如果用户查看状态栏并看到一个图标,她应该有办法弄清楚这个图标的含义。因此,通知面板中必须有一行对应于每个图标。
我建议创建一个通知来解释,就像你在你的问题中所做的那样,同步正在进行中;这是一个很好的机会来指示哪个应用程序目前正在同步(所以如果有某种问题或同步是永久的用户知道哪个应用程序需要注意)。
您可以使用此方法在状态栏上显示通知图标
private void showNotification() {
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// In this sample, we'll use the same text for the ticker and the
// expanded notification
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher,
"Service Started", System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.service_label),
"Service Started", contentIntent);
// Send the notification.
// We use a layout id because it is a unique number. We use it later to
// cancel.
nm.notify(R.string.service_started, notification);
}
好吧,这是可能的InputMethodService,只需使用showStatusIcon(resource_name)。但我认为这不是一个好主意,除了输入服务。
Context context = TutListDownloaderService.this
.getApplicationContext();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(NOTIFICATION_SERVICE);
创建通知:
Notification updateComplete = new Notification();
updateComplete.icon = android.R.drawable.stat_notify_sync;
updateComplete.tickerText = context.getText(R.string.notification_title);
updateComplete.when = System.currentTimeMillis();
创建挂起对象:
Intent notificationIntent = new Intent(context,TutListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
String contentTitle = context.getText(R.string.notification_title).toString();
String contentText;
if (!result) {
Log.w(DEBUG_TAG, "XML download and parse had errors");
contentText = context.getText(R.string.notification_info_fail).toString();
} else {
contentText = context.getText(
R.string.notification_info_success).toString();
}
updateComplete.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
最后显示通知给用户:
notificationManager.notify(LIST_UPDATE_NOTIFICATION, updateComplete);