How to use ACTION_SET_TIMER intent on android studio

Viewed 80

I'm trying to set a timer on the phone (not in the app I created) using ACTION_SET_TIMER intent and I'm not sure why it's not working when I click the button it either restarts the app or crashes it. Thanks for the help!

<Button
    android:id="@+id/timer_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:onClick="startTimer"
    android:text="@string/timer_note"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/camera_button"
    />



public void startTimer(View view) {
    String searchFor = ((EditText) findViewById(R.id.share_edittext)).getText().toString();
    String strNew = searchFor.replaceFirst("You ordered a ", "");
    int seconds = 10;
    Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER)
            .putExtra(AlarmClock.EXTRA_MESSAGE, strNew)
            .putExtra(AlarmClock.EXTRA_LENGTH, seconds)
            .putExtra(AlarmClock.EXTRA_SKIP_UI, true);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
    else {
        Log.d("ImplicitIntents", "Can't handle this intent!");
    }
}
1 Answers

After further digging I realized I needed to add a permission to the Manifest.XML file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.homework">
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
Related