Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active January 29, 2024 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunmeat/79d0e00299a536420c5b4b2e2ab51c17 to your computer and use it in GitHub Desktop.
Save sunmeat/79d0e00299a536420c5b4b2e2ab51c17 to your computer and use it in GitHub Desktop.
alarm android example
AndroidManifest.xml:
...
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
...
==============================================================================================
MainActivity.java:
package com.sunmeat.alarm;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimePicker timePicker = findViewById(R.id.timePicker);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
timePicker.setIs24HourView(true);
}
Button setAlarmButton = findViewById(R.id.setAlarmButton);
Calendar currentTime = Calendar.getInstance();
currentTime.add(Calendar.MINUTE, 1);
timePicker.setHour(currentTime.get(Calendar.HOUR_OF_DAY));
timePicker.setMinute(currentTime.get(Calendar.MINUTE));
setAlarmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hour = timePicker.getHour();
int minute = timePicker.getMinute();
setAlarm(hour, minute);
}
});
}
private void setAlarm(int hour, int minute) {
Intent intent = new Intent(android.provider.AlarmClock.ACTION_SET_ALARM)
.putExtra(android.provider.AlarmClock.EXTRA_HOUR, hour)
.putExtra(android.provider.AlarmClock.EXTRA_MINUTES, minute)
.putExtra(android.provider.AlarmClock.EXTRA_MESSAGE, "Пора делать ДЗ!")
.putExtra(android.provider.AlarmClock.EXTRA_SKIP_UI, true);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
==============================================================================================
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/setAlarmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timePicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Установить будильник" />
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment