单击通知关闭警报声音

时间:2019-08-13 03:51:26

标签: java android

我正在创建一个带有声音和通知的警报。我想通过单击通知来关闭警报声。我可以使用什么代码来做到这一点?

这是我的警报活动(我使用它来设置和取消警报)

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm);

    mTextView = findViewById(R.id.text_view);

    Button buttonTimePicker = findViewById(R.id.set_alarm);
    buttonTimePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment timePicker = new TimePickerFragment();
            timePicker.show(getSupportFragmentManager(), "Time Picker");
        }
    });

    Button buttonCancelAlarm = findViewById(R.id.cancel_alarm);
    buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cancelAlarm();
        }
    });
}

@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);a

    updateTimeText(c);
    startAlarm(c);
}

private void updateTimeText(Calendar c) {
    String timeText = "Alarm diatur pada : ";
    timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());

    mTextView.setText(timeText);
}

private void startAlarm(Calendar c) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlertReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);

    if (c.before(Calendar.getInstance())) {
        c.add(Calendar.DATE,1);
    }

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}

private void cancelAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent =  new Intent(this, AlertReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);

    alarmManager.cancel(pendingIntent);
    mTextView.setText("Alarm Dibatalkan");
}

这是我的警报接收者

@Override
public void onReceive(Context context, Intent intent) {

    NotificationHelper notificationHelper = new NotificationHelper(context);

    NotificationCompat.Builder nb = notificationHelper.getChannelNotification();

    notificationHelper.getManager().notify(1, nb.build());

    // intent to ringtone service
    Intent service_intent = new Intent(context, RingtonePlayingService.class);

    // start ringtone service
    context.startService(service_intent);

    // stop ringtone service
    context.stopService(service_intent);
}

这是我的通知助手类

public class NotificationHelper extends ContextWrapper {

public static final String channelID = "channelID";

public static final String channelName = "Channel Name";

private NotificationManager mManager;

public NotificationHelper(Context base) {
    super(base);

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

@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {

    NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);

    getManager() .createNotificationChannel(channel);
}

public NotificationManager getManager() {
    if (mManager == null) {
        mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    return mManager;
}

public NotificationCompat.Builder getChannelNotification () {
    Intent resultIntent = new Intent(this, OlahragaActivity.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    return new NotificationCompat.Builder(getApplicationContext(), channelID)
            .setContentTitle("Alarm menyala!")
            .setContentText("Waktunya anda untuk berolahraga!")
            .setContentIntent(resultPendingIntent)
            .setAutoCancel(true)
            .setSmallIcon(R.mipmap.logo_icon);
}

这是我的铃声播放服务类

MediaPlayer alarm_song;

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

@Override
public int onStartCommand (Intent intent, int flags, int startId) {
    Log.i("LocalService", "Received start id " + startId + ": " + intent);

    alarm_song = MediaPlayer.create(this, R.raw.alarm_sound);
    alarm_song.setLooping(true);
    alarm_song.start();

    return START_NOT_STICKY;
}


@Override
public void onDestroy () {
    alarm_song.stop();
}

0 个答案:

没有答案