android实现具有闹铃功能的闹钟的代码和布局文件
时间: 2024-01-21 21:03:08 浏览: 127
android闹钟代码
以下是一个简单的实现具有闹铃功能的闹钟的 Android 代码和布局文件:
布局文件 alarm_clock.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textViewTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="80sp"
android:gravity="center" />
<Button
android:id="@+id/buttonSetAlarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set Alarm" />
<Button
android:id="@+id/buttonCancelAlarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel Alarm"
android:visibility="gone" />
</LinearLayout>
```
Java 代码 AlarmClockActivity.java:
```java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class AlarmClockActivity extends AppCompatActivity {
private TextView textViewTime;
private Button buttonSetAlarm;
private Button buttonCancelAlarm;
private AlarmManager alarmManager;
private PendingIntent alarmIntent;
private boolean isAlarmSet = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_clock);
textViewTime = findViewById(R.id.textViewTime);
buttonSetAlarm = findViewById(R.id.buttonSetAlarm);
buttonCancelAlarm = findViewById(R.id.buttonCancelAlarm);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
buttonSetAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAlarm();
}
});
buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelAlarm();
}
});
}
private void setAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a", Locale.getDefault());
textViewTime.setText(sdf.format(calendar.getTime()));
isAlarmSet = true;
buttonSetAlarm.setVisibility(View.GONE);
buttonCancelAlarm.setVisibility(View.VISIBLE);
}
private void cancelAlarm() {
alarmManager.cancel(alarmIntent);
textViewTime.setText("");
isAlarmSet = false;
buttonSetAlarm.setVisibility(View.VISIBLE);
buttonCancelAlarm.setVisibility(View.GONE);
}
@Override
protected void onResume() {
super.onResume();
if (isAlarmSet) {
buttonSetAlarm.setVisibility(View.GONE);
buttonCancelAlarm.setVisibility(View.VISIBLE);
} else {
buttonSetAlarm.setVisibility(View.VISIBLE);
buttonCancelAlarm.setVisibility(View.GONE);
}
}
}
```
Java 代码 AlarmReceiver.java:
```java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm triggered!", Toast.LENGTH_LONG).show();
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.alarm_sound);
mediaPlayer.start();
}
}
```
在 res/raw 文件夹下添加一个音频文件 alarm_sound.mp3。
这个代码可以让用户在界面上设置闹钟时间,并且在闹钟时间到达时播放音频文件。用户可以取消闹钟。
阅读全文