BackPressed后删除startForeground

时间:2018-11-30 15:53:30

标签: android service

我有一个PlayerService,我叫startForeground,它显示了玩家通知。它工作正常,但是当我按下PlayerActivity中的Back时,通知会自动取消。

有事吗?

我的通知仅以startForegound(int id, Notification notification)中的Service开头。

public void notification(){
    Notification notification = new Notification();
    NotificationManager notificationManager;
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    String title = "Lawan Music";
    String content = "Lawan Music Playing...";
    @DrawableRes int icon = R.mipmap.ic_launcher;
    Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        channel = new NotificationChannel("lawanmusic", "lawanmusic", NotificationManager.IMPORTANCE_HIGH);
        channel.setLightColor(getColor(R.color.colorAccent));
        channel.setSound(sound, null);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        builder.setChannelId(channel.getId());
        notificationManager.createNotificationChannel(channel);
    }
    builder.setContentTitle(title);
    builder.setContentText(content);
    builder.setSmallIcon(icon);
    builder.setAutoCancel(false);
    builder.setOngoing(true);
    builder.build();
    notification.flags = Notification.FLAG_ONGOING_EVENT|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE;
    notification = builder.build();
    startForeground(6982, notification);
}

我正在使用Android 9.0 Pie-API 28

0 个答案:

没有答案