S
S
ScarletPhoenix75262019-03-14 09:54:00
Java
ScarletPhoenix7526, 2019-03-14 09:54:00

The Ringtone is fired immediately after setting the time from the TimePickerDialog. How can I make it work only at the selected time?

public class SettingsListScheduleActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout mLlComplex;
    private LinearLayout mLlTime;
    private LinearLayout mLlSounds;
    private LinearLayout mLlFrequency;
    private LinearLayout mllVibration;
    private TextView mSelectedTime;
    private int mGetHour = 0;
    private int mGetMinute = 0;

    private MainDB mDataBase;

    private Calendar mCalendar;
    private AlarmManager mAlarmManager;
    private Ringtone mRingtone;
    private PendingIntent mPendingIntent;
    private Intent mIntent;
    private Date mCurrentTime = Calendar.getInstance().getTime();
    private int mNowHour = mCurrentTime.getHours();
    private int mNowMinute = mCurrentTime.getMinutes();

    private Handler myHandler = new Handler();
    private Thread myThread = new Thread(new Runnable() {
        @Override
        public void run() {
            myHandler.post(new Runnable() {
                @Override
                public void run() {
                    mCalendar = Calendar.getInstance();
                    final int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
                    int minute = mCalendar.get(Calendar.MINUTE);

                    mCalendar.set(Calendar.HOUR_OF_DAY, mGetHour);
                    mCalendar.set(Calendar.MINUTE, mGetMinute);

                    getReceiver();
                }
            });
        }
    });

    private Handler mRingtoneHandler = new Handler();
    private Thread mRingtoneThread = new Thread(new Runnable() {
        @Override
        public void run() {
            mRingtoneHandler.post(new Runnable() {
                @Override
                public void run() {
                    mRingtone.play();
                }
            });
        }
    });

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

        getToolbar();
        detachClock();

        mLlTime = findViewById(R.id.llTimeSetting);
        mLlTime.setOnClickListener(this);
        mLlComplex = findViewById(R.id.llComplexSetting);
        mLlComplex.setOnClickListener(this);
        mLlSounds = findViewById(R.id.llSoundSetting);
        mLlSounds.setOnClickListener(this);
        mSelectedTime = findViewById(R.id.tvTimeSetting);

        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        mRingtone = RingtoneManager.getRingtone(getApplicationContext(), notification);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llTimeSetting:
                showTimePicker();
                break;

            case R.id.llComplexSetting:
                startActivity(new Intent(this, TaskActivity.class));
                break;

            case R.id.llSoundSetting:
                startActivity(new Intent(this, SoundsListScheduleActivity.class));
                break;

            case R.id.llFrequencySetting:
                startActivity(new Intent(this, StubActivity.class));
                break;

            case R.id.llVibrationSetting:
                startActivity(new Intent(this, StubActivity.class));
        }
    }

    public void showTimePicker() {
        final Calendar calendar = Calendar.getInstance();
        final int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        TimePickerDialog.OnTimeSetListener onTimeSetListener =
                new TimePickerDialog.OnTimeSetListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        if (view.isShown()) {
                            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                            calendar.set(Calendar.MINUTE, minute);
                            mGetHour = hourOfDay;
                            mGetMinute = minute;
                            mSelectedTime.setText(String.valueOf(mGetHour) + ":" + String.valueOf(mGetMinute));

                            if (mGetHour >= mNowHour && mGetMinute >= mNowMinute) {
                                mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                                mIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
                                mRingtone.play();
                            }

                            myThread.start();
                            mRingtone.stop();
                        }
                    }
                };

        TimePickerDialog timePickerDialog =
                new TimePickerDialog(SettingsListScheduleActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
                        onTimeSetListener,
                        hour,
                        minute,
                        true);
        timePickerDialog.setTitle("Choice your time: ");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Objects.requireNonNull(timePickerDialog
                    .getWindow())
                    .setBackgroundDrawableResource(android.R.color.transparent);
        }
        timePickerDialog.show();
    }

    public void getReceiver() {
        mPendingIntent = PendingIntent.getBroadcast(SettingsListScheduleActivity.this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), mPendingIntent);
        mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), AlarmManager.INTERVAL_HALF_HOUR, mPendingIntent);
    }

    @Override
    public void onBackPressed() {
        myThread.interrupt();
    }
}


public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= 16) {
            Intent i = new Intent(context.getApplicationContext(), AlarmActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("fast", true);
            i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            context.startActivity(i);
            Log.d("Receiver", "receiver");
        }
    }
}

I can't figure out how to do it...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question