当运行应用程序图标在状态栏 [英] Icon in the status bar when application is running

查看:174
本文介绍了当运行应用程序图标在状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个图标在状态栏中当过我的应用程序正在运行,当它在后台运行,包括。我怎样才能做到这一点?

I want to put an icon in the status bar when ever my application is running, including when it is running in the background. How can I do this?

推荐答案

您应该能够与通知和NotificationManager做到这一点。但是得到一个保证的方式来知道你的应用程序没有运行是困难的部分。

You should be able to do this with Notification and the NotificationManager. However getting a guaranteed way to know when your application is not running is the hard part.

您可以得到的基本功能是什么,你正在做的东西像渴望:

You can get the basic functionality of what you are desiring by doing something like:

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

这code必须是某个地方,你肯定当你的应用程序启动时就会被解雇。可能在你的应用程序的自定义应用程序对象的onCreate()方法。

This code must be somewhere where you are sure will get fired when your application starts. Possibly in your application's custom Application Object's onCreate() method.

后然而,事情棘手。该应用程序的杀戮随时可能发生。所以,你可以尝试将应用程序类的东西在onTerminate()也一样,但它不能保证被调用。

However after that things are tricky. The killing of the application can happen at anytime. So you can try to put something in the onTerminate() of the Application class too, but it's not guaranteed to be called.

((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);

将是所需要的,以删除图标

will be what is needed to remove the icon.

这篇关于当运行应用程序图标在状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆