- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个与通知(系统)一起运行的服务。当服务停止(和/或应用程序停止)时,应取消通知(即不再显示在状态栏上)。
正在通过提供 Android Dev guide of Notifications ,我没有找到有关如何关闭通知的任何信息。
去SO,我发现了一些问题。
1.
总结 @nipun.birla的 answer应该如何取消 Android 通知:
致 取消通知 ,按顺序尝试:
notificationID
notificationID
和 notificationTag
private Context context;
private NotificationManager notificationManager;
private NotificationChannel notificationChannel;
private NotificationCompat.Builder notificationBuilder;
public NotificationHelper(Context context) {
this.context = context;
// Init notification
// onNotificationCreate()
{
// get notification manager system service
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create notification channel
createNotificationChannel();
//Register notification channel with notification manager
notificationManager.createNotificationChannel(notificationChannel);
}
}
// Init Notification Builder
// createNotificationChannel()
{
Log.d(TAG, "createNotificationChannel: Creating notification channel");
// Define notification channel ID, Channel Name and description
String channelName = BuildConfig.FLAVOR.concat(" Notifier");
String channelDescription = context.getString(R.string.notification_description);
// Create notification channel
notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
// Set description of notification channel
notificationChannel.setDescription(channelDescription);
}
}
new NotificationHelper(getApplicationContext)
调用的其中上下文是
Application使用的上下文(也用作许多其他功能的上下文)
contentText
调用
setTextContent(String)
:
public NotificationHelper setTextContent(String text){
notificationBuilder.setContentText(text);
return this;
}
contentTitle
通过`setTitle(字符串):
public NotificationHelper setTitle(String format) {
notificationBuilder.setContentTitle(format);
return this;
}
smallIcon
(状态图标)通过拨打
setStatusIcon(int)
:
public NotificationHelper setStatusIcon(int res_id) {
notificationBuilder.setSmallIcon(res_id);
return this;
}
public void update() {
Log.d(TAG, "update: Updating notification");
Notification notification = notificationBuilder.build();
// Set notification flags
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
// Notify update
notificationManager.notify(TAG, notificationId, notification);
}
cancelNotification()
一样简单。 :
public void cancelNotification() {
Log.d(TAG, "cancelNotification: Cancelling notification");
notificationManager.cancel(TAG, notificationId);
}
unbindService
用 getApplicationContext()
调用在拥有上下文如 unbindService(ServiceConnection)使用相同的 ServiceConnection作为原来的绑定(bind)。 notificationManger.cancel(int)
notificationManger.cancel(String, int)
notificationManger.cancelAll
NotificationManager
发布这些更新的方法(即这里没有设置标志,但使用了相同的
notificationmanager
)
public void updateCancelable() {
Log.d(TAG, "update: Updating notification to cancel");
Notification notification = notificationBuilder
.setContentIntent(null)
.setOngoing(false)
.setAutoCancel(true)
.build();
// Notify update
notificationManager.notify(TAG, notificationId, notification);
}
setContentIntent(null)
并重新通知更新setAutoCancel(true)
,并设置 ongoing state与 setOngoing(false)
并重新通知更新cancelNotification()
,尽管 Android Studio 仍然保持 Activity 的调试 session ,但应用程序绝不应该再运行当应用程序仍在运行时,像人们期望的那样打开。不确定这是否与它有关
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.connectedover.BuildConfig;
import com.connectedover.R;
import com.connectedover.listeners.NotificationUpdateListener;
/**
* Class aimed at providing helper method for creating, maintaining and destroying notifications in conjunction with {@link de.blinkt.openvpn.core.OpenVPNService}
*
* @author cybex
* @since 1.5.1
*/
public class NotificationHelper implements NotificationUpdateListener {
private static final String TAG = NotificationManager.class.getSimpleName();
private static final String channelId = BuildConfig.APPLICATION_ID.concat(".").concat(TAG);
private static final int notificationId = 42;
private Context context;
private NotificationManager notificationManager;
private NotificationChannel notificationChannel;
private NotificationCompat.Builder notificationBuilder;
public NotificationHelper(Context context) {
this.context = context;
// Init notification
onNotificationCreate();
// Init Notification Builder
createBasicNotification();
}
/**
* Initialize {@link NotificationChannel} and register channel with {@link NotificationManager} service if API is Android Orea (API 26 or higher), else initializes the notification manager
*/
private void onNotificationCreate() {
Log.d(TAG, "onNotificationCreate: Initializing notification helper");
// get notification manager system service
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create notification channel
createNotificationChannel();
//Register notification channel with notification manager
notificationManager.createNotificationChannel(notificationChannel);
}
}
/**
* Creates a notification channel required by devices running Android SDK 26 and higher.
* The notification channel is set to {@link NotificationManager#IMPORTANCE_LOW} which should have no sound and appear right at the top of the status bar
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel() {
Log.d(TAG, "createNotificationChannel: Creating notification channel");
// Define notification channel ID, Channel Name and description
String channelName = BuildConfig.FLAVOR.concat(" Notifier");
String channelDescription = context.getString(R.string.notification_description);
// Create notification channel
notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
// Set description of notification channel
notificationChannel.setDescription(channelDescription);
}
/**
* Creates a basic notification using {@link android.support.v4.app.NotificationCompatBuilder} for use throughout the application
*/
private void createBasicNotification() {
// Instantiate Notification Builder
notificationBuilder = new NotificationCompat
.Builder(context, channelId)
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_logo_disconnected)
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.setOngoing(true);
}
/**
* Set the pending intent of a clickable {@link android.app.Notification} held by {@link NotificationHelper#notificationBuilder}
* @param pendingIntent Pending intent to connect to activity
* @return returns an instance of {@link NotificationHelper}
*/
public NotificationHelper setPendingIntent(PendingIntent pendingIntent){
Log.d(TAG, "setPendingIntent: Setting notification Pending intent");
notificationBuilder.setContentIntent(pendingIntent);
return this;
}
/**
* Updates the notification which is displayed for the user.
*/
public void update() {
Log.d(TAG, "update: Updating notification");
Notification notification = notificationBuilder.build();
// Set notification flags
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
// Notify update
notificationManager.notify(TAG, notificationId, notification);
}
/**
* Updates the notification {@link NotificationHelper#notificationBuilder} with new text and displays it to the user
*
* @param text new text to display
* @return returns current {@link NotificationHelper} instance for method chaining.
*/
public NotificationHelper setTextContent(String text){
notificationBuilder.setContentText(text);
return this;
}
@Override
public void onUpdate(String update) {
Log.d(TAG, "onUpdate: updating notification via callback");
this.setTextContent(update)
.update();
}
/**
* Sets a new icon for the notification displayed to the user
* @param res_id icon resource
* @return current instance
*/
public NotificationHelper setLargeIcon(int res_id) {
notificationBuilder.setLargeIcon(ImageUtils.toBitmap(context, res_id));
return this;
}
/**
* Sets a new icon for the notification displayed to the user show in the status bar (i.e. the small icon)
* @param res_id icon resource
* @return current instance
*/
public NotificationHelper setStatusIcon(int res_id) {
notificationBuilder.setSmallIcon(res_id);
return this;
}
public NotificationHelper setTitle(String format) {
notificationBuilder.setContentTitle(format);
return this;
}
/**
* Cancels the application notification
*/
public void cancelNotification() {
Log.d(TAG, "cancelNotification: Cancelling notification");
notificationManager.cancelAll();
}
}
最佳答案
如果您想使用您的 Notification
作为前台服务的一部分,不要直接操作标志,使用 startForeground()
和 stopForeground()
在您的 Service
.
关于android - 以编程方式取消(和隐藏)Android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54066289/
是否可以使用标准输入/标准输出在 bash 中压缩/解压缩字符串? 我试过了,但显然不支持它? hey=$(echo "hello world" | gzip -cf) echo $hey # ret
我的任务是让一个企业网站适用于 IE7,它必须“足够好”,因此我禁用了任何导致问题的花哨/非必要功能。 其中之一是正在使用的搜索栏,需要进行哪些搜索,我猜测幕后某个地方有某种 JavaScript 用
我有一个执行大量处理的小程序。您可以通过按回车键打印进度。 我实现它的方法是在主线程中完成处理,同时我有一个 pthread 不断循环 getchar() 以等待输入键。 问题是当我完成处理时。发生这
我完全理解 suspendCoroutine 与 suspendCancellableCoroutine 在我的示例中的工作方式。但我想知道为什么 println("I finished") (第 1
我是 QT 的新手。目前在我的项目中我实现了 QFileDialog . 在我的用例中:每当用户选择一个文本文件时,它都会执行 functionA .但是,我发现如果在文件对话框中单击取消,funct
我有代码,仅在用户选择“另存为”时运行。为此并获取我正在使用的文件的新名称 Application.GetSaveAsFilename功能。 我遇到的问题是类型不匹配,同时检查用户是否在他没有这样做时
我的 UILocalNotification 有问题。 我正在用我的方法安排通知。 - (void) sendNewNoteLocalReminder:(NSDate *)date alrt:(NS
祝你有美好的一天 我有一个网站,其中有很多“工具提示”。这些工具提示是在将鼠标悬停在文本的特定部分上时创建的。工具提示是一个 div block ,它显示在网站上所有其他内容的顶部,并且当光标从文本移
我遇到以下问题。每隔 2 秒,程序就会进入 if 语句。在这个 if 语句中,我想要一个计时器,它会在 15 秒后给我一条消息。计时器应延迟 1 秒运行。但是当我用计时器“等待”时,if 语句将再执行
基本上我有以下代码片段, (let [task (FutureTask. fn) thr (Thread. task)] (.start thr) ;;wait for signa
取消正在进行的 ASIHttpRequest 请求的正确位置在哪里?这就是我取消的方式,但是当我 时它继续崩溃在不让请求完成的情况下从一个 View Controller 转移到另一个 View Co
我在我的 winforms 应用程序中使用 BackgroundWorker 来执行另一个类中发生的长时间运行的任务(执行数据库操作)。由于所有工作都是在另一个类中完成的,因此取消并不那么简单。我在另
我正在使用 OneSignal 向我的用户显示通知。通知工作正常,但我注意到,如果我在通知栏中“滑动”取消通知,则通知将永远保留,这是一张显示应用程序图标上的通知的图像,我想在应用程序已打开: 我看到
正在运行的 AsyncTask 的 .cancel(boolean) 方法如何工作?这是文档: Attempts to cancel execution of this task. This atte
我注意到,当我激活约束时,我会立即在该行代码处收到一条警告,指出不能同时满足约束。 我假设布局是在“UI 更新周期”之类的稍后时间点计算的,而不是每次约束都被(取消)激活。因此,在(取消)激活约束的代
这是我创建线程的方式: readFromWebThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadThread:
我目前正在尝试取消与我的数据模型中的对象关联的特定 UILocalNotifications。为此,每个数据对象都有一个唯一标识符,即 NSUUID。 创建 UILocalNotification:
当我提交并单击“确定”时,它会继续,但当我按“取消”时,它仍然会提交。我尝试使用此代码,但提交和取消按钮仍然执行相同的操作。 model.saveForm = function() { var
我有一个警报弹出窗口,当发生特定操作时会出现该弹出窗口。 5 秒后,使用 setTimeout() 隐藏警报弹出窗口。 我遇到的问题是,如果我多次触发弹出窗口,有时后续的弹出窗口会出现但立即消失。我相
我有一些 javascipt (jQuery),其中单击按钮时会淡入 #myDiv,然后使用超时函数在 5 秒后再次淡出。它工作正常,但如果用户在超时内的 fadeOut 函数运行之前再次单击该按钮,
我是一名优秀的程序员,十分优秀!