Android在没有可扩展视图的状态栏中显示通知

问题描述:

我需要帮助来做些事情。 我尝试在Android状态栏中显示通知。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。

任何人都知道该怎么做? 在此先感谢!

+0

我看见几个答案从CommonsWare是说这是不可能的,但我也在s中看到各种应用程序(例如雅虎邮箱),它使 – 2013-04-09 05:05:27

这是不可能的通知图标。

原理是这样的:如果用户看着状态栏并看到一个图标,她应该有一些方法来确定该图标的含义。因此,每个图标对应的通知面板中必须有一行。

我建议创建一个通知,解释正如您在问题中所做的那样,正在进行同步;这是一个很好的机会来指出哪个应用程序正在同步(所以如果出现某种问题或者同步持续进行,用户知道哪个应用程序需要关注)。

+0

感谢您的回答。 我看到Yahoo Mail没有消耗视图发出通知,所以很奇怪... – 2013-04-10 14:51:05

+0

真的吗?在通知栏中无法创建没有行的图标是不可能的。你能指点我一个截图什么的? – dsandler 2013-04-18 15:21:04

+0

我错了Yahoo Mail ..雅虎应用程序显示的图标是操作系统显示的同步图标。 我用不同的方式来通知用户在应用程序执行服务器连接;-) 谢谢! – 2013-04-25 12:57:57

您可以使用此方法来显示在状态栏上

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); 

} 
+0

感谢您的答案,但它不是我想要的。 你的代码和我一样(并且这种实例化通知的方式已被废弃)。我不希望在通知面板中显示一个视图,但只显示状态栏中的小图标 如果我没有放置通知面板的视图,则会发生以下异常: 'java.lang.IllegalArgumentException:contentView required :pkg = com.test.app.notification id = 1 notification = Notification(pri = 0 contentView = null vibrate = null sound = null defaults = 0x0 flags = 0x0 kind = [null]) – 2013-04-09 05:48:31

那么,这是可能的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);