【问题标题】:How to add my app's icon into the status bar when my app is running?当我的应用程序运行时,如何将我的应用程序的图标添加到状态栏中?
【发布时间】:2013-06-02 21:31:28
【问题描述】:

我尝试使用 Notification.BuilderNotification 类。

我尝试使用此代码:

Notification notification = new Notification.Builder(this).build();
notification.icon = R.drawable.ic_launcher;
notification.notify();

不过好像没什么用。

我只想将我的应用程序图标添加到电池图标、wifi 图标和 3g 图标旁边。有什么办法吗?感谢您的帮助。

【问题讨论】:

标签: android


【解决方案1】:

您必须在描述完通知后调用 build() 方法。以Android reference 为例。

基本上,您必须将代码更改为以下内容:

Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context )
.setSmallIcon(R.drawable.ic_launcher);      

Intent intent = new Intent( context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, mID , intent, 0);
builder.setContentIntent(pIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notif = builder.build();
mNotificationManager.notify(mID, notif);

注意:此代码仅允许在通知栏中显示您的图标。如果你想让它在那里持续存在,你将不得不使用FLAG_ONGOING_EVENT

【讨论】:

  • 之前的代码只是简单地显示正确的顺序来显示图标。我编辑了答案以反映您的评论。
  • 这并没有将图标添加到电池和信号图标所在的右侧。
  • @JamerTheProgrammer 在这种情况下,这个问题可能会有用:stackoverflow.com/questions/18350016/…
【解决方案2】:

您可以为状态栏通知添加应用图标。试试这个

Notification notification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).build();

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多