Java 使通知可单击且不可删除
我试图创建一个通知,通过点击它来启动一个活动,而你不能将它刷走Java 使通知可单击且不可删除,java,android,android-intent,notifications,android-pendingintent,Java,Android,Android Intent,Notifications,Android Pendingintent,我试图创建一个通知,通过点击它来启动一个活动,而你不能将它刷走 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .setSmallIc
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1234567890;
mNotificationManager.notify(mId, mBuilder.build());
通过上面的代码,通知会显示出来,但当我单击它时,什么也不会发生,您可以将其刷走。您想要一个前台服务,详细信息如下
值得注意的是,只有安卓4.3添加了您可能需要的持久通知,用户设置可以覆盖该通知。您需要一个详细的前台服务 值得注意的是,只有安卓4.3添加了您可能需要的持久通知,并且它可以被用户设置覆盖。这样做:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(YourActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = mBuilder.build();
noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, noti);
这样做:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(YourActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = mBuilder.build();
noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, noti);
试着像这样使用
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
在MainActivity.java中
不要使用你的通知;ie,而不是在onDestro()下的oncreate()调用中调用2行以下的代码
试着像这样使用
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
在MainActivity.java中
不要使用你的通知;ie,而不是在onDestro()下的oncreate()调用中调用2行以下的代码
要保留通知,用户不能以任何方式单击它,请添加此标志
Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);
要开始一项活动,您必须根据您的意图使用此标志
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
要保留通知,用户不能以任何方式单击它,请添加此标志
Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);
要开始一项活动,您必须根据您的意图使用此标志
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
您可以使用
NotificationCompat.Builder
的.setconsuming(true)
,如下使用:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle("Downloading Something")
.setContentText("Downloading")
.setSound(null)
.setDefaults(0)
.setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"
您可以使用
NotificationCompat.Builder
的.setconsuming(true)
,如下使用:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle("Downloading Something")
.setContentText("Downloading")
.setSound(null)
.setDefaults(0)
.setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"
问题主要是出于意图……如果它是不可删除的,那么你将如何删除它呢?@bofredo当应用程序关闭时。我的意思是,例如,像avast notification,您没有尝试过它,但可能是您正在寻找的?如果您使用带有通知的服务,您应该使用ForegroundsService!!问题主要是出于意图……如果它是不可删除的,那么你将如何删除它呢?@bofredo当应用程序关闭时。我的意思是,例如,像avast notification,您没有尝试过它,但可能是您正在寻找的?如果您使用带有通知的服务,您应该使用ForegroundsService@NathanMaier这段代码在服务中运行吗?您希望通过单击开始哪个活动,即您离开应用程序时的上一个活动?是的,它在服务中,但现在可以工作(我添加了
noti.contentIntent=pendingent;
)@NathanMaier此代码是否在服务中运行?您希望通过单击哪个活动开始,即您离开应用程序时的上一个活动?是的,它在服务中,但现在可以工作(我添加了noti.contentIntent=pendingent;
)