【问题标题】:Forground service and workmanager notification continues to show even after stopping the workmanager即使停止工作管理器,前台服务和工作管理器通知也会继续显示
【发布时间】:2021-09-18 18:35:09
【问题描述】:

这是一个将 workmanager 作为前台服务的简单示例。问题是即使在工作管理器停止后,前台通知也会显示。我觉得这个问题与协程内存泄漏有关。

@HiltWorker
class MyForegroundWorker @AssistedInject constructor(
    @Assisted context: Context,
    @Assisted workerParams: WorkerParameters
) : CoroutineWorker(context, workerParams) {

    private lateinit var notificationBuilder: NotificationCompat.Builder
    private var i: Long = 0

    private val notificationManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    override suspend fun doWork(): Result =  withContext(Dispatchers.IO) {
        setForeground(createForegroundInfo())
        return@withContext runCatching {

            while (true) {
                updateNotification(i.toString())
                i += 1

            }
            Result.success()
        }.getOrElse {

            Log.d("TAG", "doWork: ${it.message}, ${i}")
            Result.failure()
        }
    }


//..


}

【问题讨论】:

  • 您好,您用于显示通知的 while 循环没有退出条件。它总是正确的。因此,工人总是在循环中。这是我从代码 sn-p 中看到的。如果我错了,请纠正我。谢谢
  • 是的,停止工作管理器的唯一方法是取消它。

标签: android-service android-notifications kotlin-coroutines android-workmanager


【解决方案1】:

使用ForegroundService类:

public class ForegroundService extends Service {

    private static final String TAG = "ForegroundService";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        stopForegroundService();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Used only in case of bound services.
        return null;
    }

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

        // Handle application closing


        // Destroy the service
        stopSelf();
    }

    public static class NotificationCloseButtonHandler extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Close Clicked", Toast.LENGTH_SHORT).show();

            ObLogger.i(TAG, "onReceive: -->>");
            int notificationId = intent.getIntExtra("notificationId", 0);

            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancelAll();
        }
    }

    private void stopForegroundService() {

        stopForeground(true);
        stopSelf();
    }
}

AndroidManifest.xml

<service android:name="com.ui.ForegroundService"
            android:stopWithTask="false">
        </service>

onCreateSplash screen 上的服务电话:

startService(new Intent(getBaseContext(), ForegroundService.class));

onResume的MainActivity调用取消所有Notitfication。

NotificationManagerCompat.from(this).cancelAll();

【讨论】:

  • 我更喜欢workmanager,但如果没有其他人回答,我会给你奖励。
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 2018-10-24
  • 1970-01-01
  • 2018-04-29
  • 1970-01-01
相关资源
最近更新 更多