通知中的操作按钮正在显示,但按下时不起作用

时间:2019-07-10 03:00:32

标签: android android-service

显示通知后,我正在使用一项服务来启动媒体播放器,然后在按下操作按钮时停止该服务。该服务可以正常启动。但是,该服务不会在单击操作按钮时停止。

这是我的通知生成器

public NotificationCompat.Builder getReminderNotification(String title,String message,PendingIntent intent1){

    return new NotificationCompat.Builder(getApplicationContext(),reminderChannelID)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.drawable.ic_medicine)
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setOngoing(true)
            .addAction(R.drawable.ic_arrow_back,"OK",intent1);
}

public void cancelNotification() {
   NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(1); // Notification ID to cancel
}

public void startMyService(){
    startService(new Intent(this, TimerService.class));
}

public void stopMyService(){
    stopService(new Intent(this,TimerService.class));
}

这是我的提醒接收者:

public class ReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    NotificationHelper notificationHelper = new NotificationHelper(context);
    Intent newIntent = new Intent(context,DismissReminderReceiver.class);
    newIntent.putExtra("action","stop");
    PendingIntent intent1 = PendingIntent.getBroadcast(context,0,newIntent,0);
    Bundle extras = intent.getExtras();

    String title=extras.getString("title");
    String message=extras.getString("message");

        NotificationCompat.Builder builder = notificationHelper.getReminderNotification(title, message,intent1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context.getApplicationContext(),TimerService.class));
        }else {
            notificationHelper.startMyService();
        }
        notificationHelper.getManager().notify(1, builder.build());

}}

这是我关闭通知的代码

public class DismissReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("Here");
    Bundle extras = intent.getExtras();
    String action = extras.getString("action");
    System.out.println(action);
    if (action.equals("stop")) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        notificationHelper.stopMyService();
        notificationHelper.cancelNotification();
    }
}}

这是我的服务:

public class TimerService extends Service {

public Context context = this;
public Handler handler = null;
public Runnable runnable = null;
MediaPlayer mediaPlayer;public TimerService(){}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
System.out.println("Service Started");
    mediaPlayer=MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            mediaPlayer.start();
        }
    };
    handler.postDelayed(runnable,0);
}

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

@Override
public void onDestroy() {
    handler.removeCallbacks(runnable);
    mediaPlayer.stop();
}}

1 个答案:

答案 0 :(得分:0)

我也看不到您在哪里注册接收器-如果您在清单中进行接收,请确保它具有匹配的动作(接收器节从此处开始)。

相关问题