如何在应用程序运行时在状态栏中显示图标,包括在后台?

Nul*_*ion 26 icons android statusbar uistatusbar

我想在应用程序运行时在状态栏中放置一个图标,包括它在后台运行时.我怎样才能做到这一点?

Gre*_*lli 19

您应该可以使用Notification和NotificationManager执行此操作.然而,获得有保证的方式来了解您的应用程序何时未运行是困难的部分.

通过执行以下操作,您可以获得所需内容的基本功能:

Notification notification = new Notification(R.drawable.your_app_icon,
                                             R.string.name_of_your_app, 
                                             System.currentTimeMillis());
notification.flags |= Notification.FLAG_NO_CLEAR
                   | Notification.FLAG_ONGOING_EVENT;
NotificationManager notifier = (NotificationManager)
     context.getSystemService(Context.NOTIFICATION_SERVICE);
notifier.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)

此代码必须位于您确定将在应用程序启动时被触发的位置.可能在您的应用程序的自定义应用程序对象的onCreate()方法中.

然而事情发生之后很棘手.杀死应用程序可以随时进行.所以你也可以尝试在Application类的onTerminate()中加入一些东西,但是不能保证它被调用.

((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
Run Code Online (Sandbox Code Playgroud)

将是删除图标所需的内容.

  • API级别11中不推荐使用"Notification".请改用Notification.Builder. (5认同)

mjo*_*osh 9

对于新的API,您可以使用NotificationCompat.Builder -

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle("Title");
Intent resultIntent = new Intent(this, MyActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

只要您的应用程序正在运行并且有人手动关闭您的应用程序,它就会显示.您可以随时致电取消通知 -

mNotifyMgr.cancel(NOTIFICATION_ID);
Run Code Online (Sandbox Code Playgroud)


Bri*_*ian 6

请查看开发指南" 创建状态栏通知 ".

实现保持应用程序运行时有只有当图标的目标的方法之一是在初始化的通知onCreate(),并调用cancel(int)你的onPause()方法只有isFinishing()返回true.

一个例子:

private static final int NOTIFICATION_EX = 1;
private NotificationManager notificationManager;

@Override
public void onCreate() {
    super.onCreate();

    notificationManager = (NotificationManager) 
        getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.notification_icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
        0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, 
        contentText, contentIntent);

    notificationManager.notify(NOTIFICATION_EX, notification);
}

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        notificationManager.cancel(NOTIFICATION_EX);
    }
}
Run Code Online (Sandbox Code Playgroud)