我有一个Timepicker空件和一个imagebutton,实现选择好时间,点击imagebutton开启闹钟,到达timepicker所选的时间播放MP3音乐,再次点击imagebutton可以关闭闹钟并且暂停正在播放的MP3音乐,请帮我写完整的可以运行的代码。
时间: 2023-09-17 20:06:18 浏览: 168
### 回答1:
好的,以下是可以实现您要求的代码:
```
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private TimePicker timePicker;
private ImageButton imageButton;
private MediaPlayer mediaPlayer;
private PendingIntent pendingIntent;
private AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = findViewById(R.id.timePicker);
imageButton = findViewById(R.id.imageButton);
// 初始化闹钟和音乐播放器
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mediaPlayer = MediaPlayer.create(this, R.raw.music);
// 设置点击事件
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (pendingIntent != null) {
// 如果已经设置了闹钟,则取消闹钟并停止音乐播放
alarmManager.cancel(pendingIntent);
mediaPlayer.stop();
Toast.makeText(MainActivity.this, "闹钟已关闭", Toast.LENGTH_SHORT).show();
pendingIntent = null;
} else {
// 如果没有设置闹钟,则设置闹钟并开始音乐播放
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, );
calendar.set(Calendar.MILLISECOND, );
long timeInMillis = calendar.getTimeInMillis();
if (timeInMillis < System.currentTimeMillis()) {
timeInMillis += 24 * 60 * 60 * 100;
}
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, , intent, );
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
mediaPlayer.start();
Toast.makeText(MainActivity.this, "闹钟已设置", Toast.LENGTH_SHORT).show();
}
}
});
}
// 定义广播接收器,用于接收闹钟触发事件
public static class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 停止音乐播放
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music);
mediaPlayer.stop();
mediaPlayer.release();
// 显示闹钟触发提示
Toast.makeText(context, "闹钟响了!", Toast.LENGTH_LONG).show();
}
}
}
```
需要注意的是,您还需要在布局文件中添加 TimePicker 和 ImageButton 控件,并为 ImageButton 控件设置一个名为 `imageButton` 的 ID。此外,您还需要在 AndroidManifest.xml 文件中注册广播接收器,代码如下:
```
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarm">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MainActivity$AlarmReceiver" />
</application>
</manifest>
```
### 回答2:
要实现这个功能,你需要在你的项目中添加一个TimePicker控件和一个ImageButton控件。然后,你可以按照以下步骤编写代码以实现所需的功能:
1. 在布局文件中添加TimePicker和ImageButton控件:
```xml
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/alarmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/alarm_icon" />
```
2. 在活动的Java文件中引用和初始化控件:
```java
TimePicker timePicker = findViewById(R.id.timePicker);
ImageButton alarmButton = findViewById(R.id.alarmButton);
```
3. 为ImageButton控件添加点击事件监听器,并在其中实现开启和关闭闹钟的逻辑:
```java
boolean isAlarmSet = false; // 判断闹钟是否已设置
MediaPlayer mediaPlayer; // 播放MP3音乐的MediaPlayer对象
alarmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isAlarmSet) {
// 关闭闹钟逻辑
alarmButton.setImageResource(R.drawable.alarm_icon);
stopAlarm();
isAlarmSet = false;
} else {
// 开启闹钟逻辑
alarmButton.setImageResource(R.drawable.alarm_icon_active);
startAlarm();
isAlarmSet = true;
}
}
});
// 开启闹钟的方法
private void startAlarm() {
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
// 设置闹钟逻辑,使用AlarmManager等方法实现
// 播放MP3音乐
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.alarm_sound);
mediaPlayer.start();
}
// 关闭闹钟的方法
private void stopAlarm() {
// 关闭闹钟逻辑,取消之前设置的闹钟
// 停止播放MP3音乐
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
```
通过以上步骤,你可以实现根据TimePicker选择的时间,点击ImageButton开启和关闭闹钟,并且在闹钟到达时间后播放MP3音乐。
### 回答3:
下面是一个可以实现你描述的功能的Android代码示例:
1. 在布局文件中添加一个TimePicker和一个ImageButton:
```xml
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/alarmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/alarm_icon" />
```
2. 在MainActivity中实现相关逻辑:
```java
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TimePicker;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TimePicker timePicker;
private ImageButton alarmButton;
private Timer timer;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = findViewById(R.id.timePicker);
alarmButton = findViewById(R.id.alarmButton);
alarmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timer != null) {
timer.cancel();
timer = null;
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
alarmButton.setImageResource(R.drawable.alarm_icon); // 设置按钮图标
} else {
int hour = timePicker.getCurrentHour(); // 获取选择的小时数
int minute = timePicker.getCurrentMinute(); // 获取选择的分钟数
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
long delayInMillis = calendar.getTimeInMillis() - System.currentTimeMillis();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.alarm_sound);
mediaPlayer.start();
}
}, delayInMillis);
alarmButton.setImageResource(R.drawable.stop_alarm_icon); // 设置按钮图标
}
}
});
}
}
```
在上述代码中,我们使用java.util.Timer和java.util.TimerTask来实现闹钟的定时功能。当点击ImageButton时,我们根据选择的时间计算定时器的延迟时间,在该时间达到后播放音乐。当再次点击ImageButton时,我们取消定时器并停止播放音乐。
阅读全文