Android 创建持久通知并阻止状态栏中的通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12205551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a persistent notification and prevent notification in status bar
提问by Dino
I have the following code which I am using for an android app:
我有以下代码用于 android 应用程序:
package com.authorwjf.e_notifications;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.avatar),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height),
true);
Intent intent = new Intent(this, Main.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 01, intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle("This is the title");
builder.setContentText("This is the text");
builder.setSubText("Some sub text");
builder.setNumber(101);
builder.setContentIntent(pendingIntent);
builder.setTicker("Fancy Notification");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(bm);
builder.setAutoCancel(true);
builder.setPriority(0);
Notification notification = builder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(01, notification);
}
}
Basically the app when launched creates a notification, now I have a few questions:
基本上,应用程序在启动时会创建一个通知,现在我有几个问题:
- Is it possible when the app is launched that the notification is displayed in the the pull down notification list but without the icon in the status bar. ie. refer to
- 是否有可能在启动应用程序时,通知显示在下拉通知列表中,但状态栏中没有图标。IE。参考
can the icon circled in red not be displayed? Ultimately I would like to create a service that just sits in the pull down notification.
红圈图标可以不显示吗?最终,我想创建一个仅位于下拉通知中的服务。
- Is there away to make the notification in the pulldown persist ie. if the icon circled in blue (http://imagebin.org/226494) is pressed that the notification still remains?
- 是否可以使下拉菜单中的通知保持不变,即。如果按下蓝色圆圈图标 ( http://imagebin.org/226494),通知仍然存在?
I am new to android dev and just trying to get to grips with what I can do with notifications.
我是 android 开发的新手,只是想掌握我可以用通知做什么。
Thanks
谢谢
回答by iTurki
A1: To remove the status bar icon, use this trick:
builder.setSmallIcon(android.R.color.transparent); //Tested and worked in API 14
A2: To make persistant Notification, add this line:
builder.setOngoing(true)
A1:要删除状态栏图标,请使用以下技巧:
builder.setSmallIcon(android.R.color.transparent); //Tested and worked in API 14
A2:要进行持久化通知,请添加以下行:
builder.setOngoing(true)
回答by Daniel Smith
Take a look at the ongoing event flag. I believe this can create ongoing notifications similar to the system's wifi and usb connection notifications.
看看正在进行的事件标志。我相信这可以创建类似于系统的 wifi 和 USB 连接通知的持续通知。