Matikan suara alarm lewat klik notifikasi

Halo, saya ingin menanyakan source code untuk aplikasi single alarm saya. saya sudah membuat alarm + suara + notifikasi. namun ada kendala ketika saya ingin mematikan suara alarm melalui klik atau tap notifikasi. untuk bisa mematikan suara alarm melalui notifikasi, saya harus menambahkan kode dimana ya?

saya sudah menyiapkan code untuk set + cancel alarm, alert reciever, notification dan ringtone playing.

ini adalah code untuk set dan cancel alarm

public class AlarmActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {

    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);

        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");
    }

ini adalah code Alert Reciever

public class AlertReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1, nb.build());

        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(3000);

        // 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);
    }

ini adalah code Notifikasi

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);
    }

dan terakhir ini adalah code untuk ringtone playing service

public class RingtonePlayingService extends Service {

    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();
    }

avatar chakranugraha
@chakranugraha

1 Kontribusi 0 Poin

Diperbarui 1 tahun yang lalu

Tanggapan

di sekolah koding ada kelas android ?

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban