通知栏开/关切换点击

时间:2018-02-10 11:14:34

标签: android

如何通过点击它来启动和停止通知服务(没有按钮),我以某种方式设法启动挂起的意图,但不知道放在哪里停止服务()/ stopself()< / em>命令。

PendingIntent pi = PendingIntent.getService(MainActivity.this,0, new Intent(MainActivity.this, MainService.class), 0);

    final Notification notification = new NotificationCompat.Builder(MainActivity.this)
            .setTicker("MY FIRST NOTIFICATION TOGGLE")
            .setSmallIcon(R.drawable.ic_blur_on_black_24dp)
            .setContentTitle("Click to toggle on/off")
            .setContentText("Click Again")
            .setContentIntent(pi)
            .setOngoing(true)
            .build();

    final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

1 个答案:

答案 0 :(得分:0)

在通知中添加操作:

final Notification notification = new 

NotificationCompat.Builder(MainActivity.this)
            .setTicker("MY FIRST NOTIFICATION TOGGLE")
            .setSmallIcon(R.drawable.ic_blur_on_black_24dp)
            .setContentTitle("Click to toggle on/off")
            .setContentText("Click Again")
            .setContentIntent(pi)
            .setOngoing(true)
            .addAction(R.drawable.some_icon, "start service", pi)
            .build();