android studio闹钟代码和布局文件
时间: 2023-07-27 09:19:45 浏览: 65
以下是一个简单的Android闹钟应用的代码和布局文件:
布局文件(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_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_below="@+id/time_display"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="@+id/start_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:visibility="gone"/>
</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.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class AlarmClockActivity extends AppCompatActivity {
private TextView mTimeDisplay;
private Button mStartButton;
private Button mStopButton;
private AlarmManager mAlarmManager;
private PendingIntent mPendingIntent;
private static final int MSG_UPDATE_TIME = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE_TIME:
updateTime();
break;
default:
super.handleMessage(msg);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_clock);
mTimeDisplay = findViewById(R.id.time_display);
mStartButton = findViewById(R.id.start_button);
mStopButton = findViewById(R.id.stop_button);
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
mPendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startAlarm();
}
});
mStopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopAlarm();
}
});
}
private void startAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mPendingIntent);
mStartButton.setVisibility(View.GONE);
mStopButton.setVisibility(View.VISIBLE);
updateTime();
}
private void stopAlarm() {
mAlarmManager.cancel(mPendingIntent);
mStopButton.setVisibility(View.GONE);
mStartButton.setVisibility(View.VISIBLE);
mHandler.removeMessages(MSG_UPDATE_TIME);
}
private void updateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date());
mTimeDisplay.setText(time);
mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, 1000);
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
}
```
这里还需要一个广播接收器(AlarmReceiver.java),用于接收闹钟响铃的广播:
```java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.widget.Toast;
import java.io.IOException;
public class AlarmReceiver extends BroadcastReceiver {
private MediaPlayer mMediaPlayer;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
playAlarm(context);
}
private void playAlarm(Context context) {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopAlarm();
}
});
}
try {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(context, Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.alarm));
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopAlarm() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
```
这个应用程序会在启动后显示一个当前时间的 TextView,以及一个启动闹钟的按钮。当用户点击按钮时,程序会启动一个闹钟,10 秒后触发闹钟响铃,同时显示停止闹钟的按钮。当用户点击停止按钮时,程序会取消闹钟并停止响铃。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)