我想在我的应用程序运行时在状态栏中放置一个图标,包括在后台运行时。我怎样才能做到这一点?
问问题
47093 次
4 回答
21
您应该能够使用 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);
此代码必须位于您确定在应用程序启动时会被触发的位置。可能在您应用程序的自定义应用程序对象的 onCreate() 方法中。
然而,在那之后事情就变得棘手了。应用程序的终止可能随时发生。所以你也可以尝试在 Application 类的 onTerminate() 中放入一些东西,但不能保证会被调用。
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
将是删除图标所需要的。
于 2010-10-20T03:36:52.637 回答
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);
只要您的应用程序正在运行并且有人手动关闭您的应用程序,它就会显示。您可以随时通过致电取消通知 -
mNotifyMgr.cancel(NOTIFICATION_ID);
于 2015-11-13T10:56:43.783 回答
6
查看开发指南“创建状态栏通知”。
实现仅在应用程序运行时保留图标的目标的一种方法是初始化通知并仅在返回 true时onCreate()
调用cancel(int)
您的方法。onPause()
isFinishing()
一个例子:
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);
}
}
于 2010-10-20T03:34:18.397 回答
4
真的行。我从上面的示例中创建了一个方法:
private void applyStatusBar(String iconTitle, int notificationId) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(iconTitle);
Intent resultIntent = new Intent(this, ActMain.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;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationId, notification);}
它应该被称为:applyStatusBar("Statusbar Test", 10);
于 2016-04-12T16:31:25.350 回答