在应用程序运行时,如何在状态栏中显示一个图标,包括背景?
-
09-10-2019 - |
题
我想在我的应用程序运行时(包括在后台运行时)将图标放在状态栏中。我怎样才能做到这一点?
解决方案
您应该能够通过通知和通知管理器来执行此操作。但是,获得保证的方法来知道何时不运行应用程序是困难的部分。
您可以通过做类似的操作来获得所需的基本功能:
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);
此代码必须是您确定在申请开始时启动的地方。可能在您的应用程序的自定义应用程序对象的ongreate()方法中。
但是之后,事情很棘手。该应用程序的杀戮可以随时发生。因此,您也可以尝试将某些内容放入应用程序类的OnTerminate())中,但是不能保证会被调用。
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
将是删除图标所需的内容。
其他提示
对于新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);
看一下开发指南”创建状态栏通知".
只有在应用程序运行时,才能实现将图标保留在此处的目标是初始化通知 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);
}
}
真的行。我从上面的示例中创建了一种方法:
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);}
应该称为:applatusbar(“状态栏测试”,10);
不隶属于 StackOverflow