简体   繁体   中英

How can I cancel the notification after I leave the activity?

I recently made a MediaPlayer. I made a notification that pops up after I press play and cancels itself after I press the pause button or the exit button. There is one problem tho. As we all know, every phone has its own exit button. When I press it, the notification doesn't cancel and remains. I believe I should use onDestroy but I don't know how. Maybe there are other ways.

This is most of the code I am using:

 backm4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            m4.super.onBackPressed();
            notificationManager.cancelAll();
        }
    });

    playerPosition = findViewById(R.id.playerPosition);
    playerDuration = findViewById(R.id.playerDuration);
    replay = findViewById(R.id.replay);
    forward = findViewById(R.id.forward);
    seekBar = findViewById(R.id.seekBar);
    btPause = findViewById(R.id.btPause);
    btPlay = findViewById(R.id.btPlay);

    populateTracks();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        createChannel();
    }

    btPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btPlay.setVisibility(View.GONE);

            btPause.setVisibility(View.VISIBLE);

            mediaPlayer.start();

            seekBar.setMax(mediaPlayer.getDuration());

            handler.postDelayed(runnable, 0);

            CreateNotification.createNotification(m4.this, tracks.get(0), R.drawable.pause,
                    0, tracks.size() -1);
        }
    });

    btPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btPause.setVisibility(View.GONE);

            btPlay.setVisibility(View.VISIBLE);

            mediaPlayer.pause();

            handler.removeCallbacks(runnable);

            notificationManager.cancelAll();

        }
    });
   
}

private void createChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel channel = new NotificationChannel(CreateNotification.CHANNEL_ID,
                "Diligent", NotificationManager.IMPORTANCE_LOW);

        notificationManager = getSystemService(NotificationManager.class);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}


private void populateTracks() {
    tracks = new ArrayList<>();

    tracks.add(new Track("You are watching Getting started meditation", "Press here to enter the app", R.drawable.diligent_transparent_logo));
}

@SuppressLint("DefaultLocale")
private String convertFormat(int duration) {
    return String.format("%02d:%02d"
            , TimeUnit.MILLISECONDS.toMinutes(duration)
            ,TimeUnit.MILLISECONDS.toSeconds(duration) -
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
}

Thank you in advance.

Override the onBackPressed() method and add notificationManager.cancelAll() there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM