android实现具有闹铃功能的闹钟的代码和布局文件,闹钟要能自定义时间
时间: 2024-01-21 09:03:08 浏览: 161
好的,以下是一个简单的实现具有闹铃功能的闹钟的代码和布局文件,闹钟可以自定义时间。
布局文件 alarm_clock.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/time_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/set_alarm_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置闹钟"
android:layout_below="@id/time_text_view"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
```
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.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class AlarmClockActivity extends AppCompatActivity {
private TextView mTimeTextView;
private TimePicker mTimePicker;
private Button mSetAlarmButton;
private int mHour;
private int mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_clock);
mTimeTextView = findViewById(R.id.time_text_view);
mTimePicker = new TimePicker(this);
mSetAlarmButton = findViewById(R.id.set_alarm_button);
mSetAlarmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHour = mTimePicker.getHour();
mMinute = mTimePicker.getMinute();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMinute);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(AlarmClockActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmClockActivity.this,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(AlarmClockActivity.this, "闹钟设置成功!", Toast.LENGTH_SHORT).show();
}
});
}
}
```
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, "闹钟响了!", Toast.LENGTH_LONG).show();
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.alarm_sound);
mediaPlayer.start();
}
}
```
以上代码实现了一个简单的具有闹铃功能的闹钟,可以自定义时间。当设置的时间到达时,会弹出一个 Toast 提示用户,同时播放一个音乐文件。当然,还有很多细节需要完善,比如设置闹钟的时间不能早于当前时间等等。
阅读全文