【发布时间】:2019-11-17 19:33:06
【问题描述】:
我正在尝试创建一个持续通知,该通知将在服务被销毁时出现。当用户点击通知时,服务应该重新启动,并且在服务被销毁时创建的通知应该消失。 下面是我正在使用的代码
private boolean mRunning;
@Override
public void onDestroy(){
super.onDestroy();
destroyed();
Toast.makeText(getApplicationContext(),"you are offline",Toast.LENGTH_SHORT).show();
}
private void destroyed() {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
String Notichanid="com.g.kupa";
String channelname="My Background";
NotificationChannel chan=new NotificationChannel(Notichanid,channelname,NotificationManager.IMPORTANCE_HIGH);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
assert manager!=null;
manager.createNotificationChannel(chan);
Intent notificationintnet=new Intent(this,Profile.class);
notificationintnet.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent=PendingIntent.getActivity(this,0,notificationintnet,0);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this,Notichanid);
Notification notification=notificationBuilder.setOngoing(false)
.setSmallIcon(R.drawable.menu_icon)
.setContentTitle("You are offline")
.setContentText("If you are awaiting one of your bookings to be accepted, tap this notification to get back online")
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentIntent(contentIntent)
.build();
manager.notify(19,notification);
if (!mRunning){
mRunning=true;
manager.cancel(19);
}
以 if close 为特色的代码的最后一部分是新添加的。如果没有 if 通知,通知会出现,但当服务重新启动时,它仍然存在。 但是现在我已经添加了 if 语句来删除通知,当服务被销毁时,通知甚至没有出现,有人帮忙吗?
【问题讨论】:
标签: java android service notifications