【问题标题】:Service stopForeground(false) remove notification when should not服务 stopForeground(false) 不应该删除通知
【发布时间】:2019-04-17 13:20:14
【问题描述】:

代替

停止前景(真)

打电话,

停止前景(假)

应该保持通知原样(没有正在进行的状态),除非它被用户解除/以编程方式删除。 这也应该防止通知闪烁,因为我没有重新创建通知。

但它不起作用。 stopForeground(false) 的行为与 stopForeground(true) 相同。

这是一个示例项目:

public class AudioTestService extends Service {
private static final String CHANNEL_ID = "TestChannel";
private static final int NOTIFICATION_ID = 14;
Notification mBuilder;

public AudioTestService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    // throw new UnsupportedOperationException("Not yet implemented");
    return null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    stopForeground(true);
    super.onTaskRemoved(rootIntent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Intent intentA = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentA, 0);

    Notification mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Titolo")
            .setContentText("Descrizione")
            .setContentIntent(pendingIntent)
            .setOngoing(false)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .build();
    this.mBuilder = mBuilder;
    createNotificationChannel();
    startForeground(NOTIFICATION_ID, mBuilder);

    return START_STICKY;
}

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = CHANNEL_ID;
        String description = CHANNEL_ID + "Description ";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

@Override
public void onDestroy() {
    stopForeground(false);
    //NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, mBuilder);
    super.onDestroy();
} }

activity,轻松处理按钮点击事件:

公共类 MainActivity 扩展 AppCompatActivity 实现 View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button startService = findViewById(R.id.startService);
    Button stopService = findViewById(R.id.stopService);
    Button stopNotification = findViewById(R.id.stopWithNotification);

    startService.setOnClickListener(this);
    stopService.setOnClickListener(this);
    stopNotification.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.startService:
            ContextCompat.startForegroundService(this, new Intent(this, AudioTestService.class));
            break;
        case R.id.stopService:
            finish();
            break;
        case R.id.stopWithNotification:
            stopService(new Intent(this, AudioTestService.class));
            break;
    }
}}

如果你看一下我设置的 Service 的 onDestroy() 方法

stopForeground(false);

而不是 onTaskRemoved() 方法,当应用程序从任务列表中清除时应该删除通知。

我做错了什么?

请不要将此标记为重复,我正在寻找几天的解决方案...

【问题讨论】:

标签: android service android-notifications foreground-service foregroundnotification


【解决方案1】:

不要从onDestroy() 调用stopForeground(false);,而是从活动(带有操作)发送广播以停止服务。更改您的onStartCommand 代码以检查intent 中的操作并执行startForegroundstopForeground(false);

【讨论】:

  • ...感谢您的建议。
  • 对于其他阅读本文的人,我也遇到了这个问题。似乎如果在onDestroy() 中调用stopForeground(false),那么通知仍然会被删除。所以stopForeground(false)必须在更早的时间点被调用。
猜你喜欢
  • 2017-09-25
  • 2017-01-25
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
相关资源
最近更新 更多