停止前台服务(stopForeground(false))时不应删除通知

5

不要使用

stopForeground(true)

来停止前台服务,而是应该使用

stopForeground(false)

来保留通知(不保持运行状态),直到用户手动移除或程序删除。

这样可以避免通知闪烁,因为我没有重新创建通知。

但是该方法无效。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();
} }

这个活动可以轻松地处理按钮点击事件:

public class MainActivity extends AppCompatActivity implements 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;
    }
}}

如果您查看服务的onDestroy()方法,我设置了stopForeground(false)而不是onTaskRemoved()方法,该方法应在应用程序从任务列表中清除时删除通知。我做错了什么?请不要将此标记为重复项,我已经寻找解决方案几天了...

https://developer.android.com/reference/android/app/Service.html#stopForeground(boolean) - Jonathan I
1个回答

3

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


谢谢您的建议。 - Jonathan I
对于其他读者,我也遇到了这个问题。似乎如果在onDestroy()中调用stopForeground(false),通知仍然会被删除。因此,必须在较早的时间点调用stopForeground(false) - Sam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接