android实现一个简单的音乐播放,闹钟,日程提醒的代码和布局文件
时间: 2024-02-11 09:03:57 浏览: 81
以下是一个简单的Android音乐播放器、闹钟、日程提醒的代码和布局文件,您可以参考:
布局文件 music_player_layout.xml:
```xml
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音乐播放器"/>
<Button
android:id="@+id/btn_play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放"/>
<Button
android:id="@+id/btn_pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂停"/>
<!-- 闹钟 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="闹钟"/>
<TimePicker
android:id="@+id/time_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_set_alarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设定闹钟"/>
<!-- 日程提醒 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="日程提醒"/>
<EditText
android:id="@+id/et_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入事件"/>
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TimePicker
android:id="@+id/time_picker_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_set_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设定日程提醒"/>
</LinearLayout>
```
Java代码 MusicPlayerActivity.java:
```java
public class MusicPlayerActivity extends AppCompatActivity implements View.OnClickListener {
private MediaPlayer mediaPlayer;
private Button btnPlay, btnPause;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_player_layout);
btnPlay = findViewById(R.id.btn_play);
btnPause = findViewById(R.id.btn_pause);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
mediaPlayer = MediaPlayer.create(this, R.raw.music);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_play:
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
break;
case R.id.btn_pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
```
Java代码 AlarmActivity.java:
```java
public class AlarmActivity extends AppCompatActivity implements View.OnClickListener {
private TimePicker timePicker;
private Button btnSetAlarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_layout);
timePicker = findViewById(R.id.time_picker);
btnSetAlarm = findViewById(R.id.btn_set_alarm);
btnSetAlarm.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_set_alarm:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "闹钟已设定", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
```
Java代码 AlarmReceiver.java:
```java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "闹钟响了", Toast.LENGTH_LONG).show();
}
}
```
Java代码 EventActivity.java:
```java
public class EventActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etEvent;
private DatePicker datePicker;
private TimePicker timePicker;
private Button btnSetEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_layout);
etEvent = findViewById(R.id.et_event);
datePicker = findViewById(R.id.date_picker);
timePicker = findViewById(R.id.time_picker_event);
btnSetEvent = findViewById(R.id.btn_set_event);
btnSetEvent.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_set_event:
String event = etEvent.getText().toString();
int year = datePicker.getYear();
int month = datePicker.getMonth();
int dayOfMonth = datePicker.getDayOfMonth();
int hourOfDay = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(this, EventReceiver.class);
intent.putExtra("event", event);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "日程提醒已设定", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
```
Java代码 EventReceiver.java:
```java
public class EventReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String event = intent.getStringExtra("event");
Toast.makeText(context, "事件提醒:" + event, Toast.LENGTH_LONG).show();
}
}
```
以上就是一个简单的Android音乐播放器、闹钟、日程提醒的代码和布局文件,希望能对您有所帮助。
阅读全文