You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.example.myapplication;
import android.app.Activity;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class CourseReminderActivity extends Activity {
private MediaPlayer mediaPlayer;
private Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置为对话框样式
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_course_reminder);
// 设置窗口属性
Window window = getWindow();
// 允许在锁屏上显示
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
setTurnScreenOn(true);
android.app.KeyguardManager keyguardManager = (android.app.KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager != null) {
keyguardManager.requestDismissKeyguard(this, null);
}
} else {
window.addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
);
}
// 设置窗口在最顶层
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// 设置窗口类型Android 8.0+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
} else {
window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
// 获取课程信息
String courseName = getIntent().getStringExtra("course_name");
String courseLocation = getIntent().getStringExtra("course_location");
String courseTime = getIntent().getStringExtra("course_time");
int reminderMinutes = getIntent().getIntExtra("reminder_minutes", 10);
// 显示信息
TextView tvCourseName = findViewById(R.id.tv_course_name);
TextView tvCourseInfo = findViewById(R.id.tv_course_info);
TextView tvReminderTime = findViewById(R.id.tv_reminder_time);
tvCourseName.setText(courseName);
tvCourseInfo.setText("地点:" + courseLocation + "\n时间" + courseTime);
tvReminderTime.setText(reminderMinutes + "分钟后上课");
// 设置按钮点击事件
findViewById(R.id.btn_confirm).setOnClickListener(v -> {
stopSoundAndVibration();
finish();
});
findViewById(R.id.btn_close).setOnClickListener(v -> {
stopSoundAndVibration();
finish();
});
// 播放提示音和震动
playNotificationSound();
vibrate();
}
/**
* 播放提示音
*/
private void playNotificationSound() {
try {
// 使用系统默认通知音
Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, notificationUri);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mediaPlayer.setAudioAttributes(audioAttributes);
mediaPlayer.setLooping(false);
mediaPlayer.prepare();
mediaPlayer.start();
// 播放完成后释放资源
mediaPlayer.setOnCompletionListener(mp -> {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 震动
*/
private void vibrate() {
try {
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null && vibrator.hasVibrator()) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// 震动模式:等待-震动-等待-震动
long[] pattern = {0, 300, 200, 300};
VibrationEffect effect = VibrationEffect.createWaveform(pattern, -1);
vibrator.vibrate(effect);
} else {
// 旧版本API
long[] pattern = {0, 300, 200, 300};
vibrator.vibrate(pattern, -1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 停止声音和震动
*/
private void stopSoundAndVibration() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
if (vibrator != null) {
vibrator.cancel();
vibrator = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
stopSoundAndVibration();
}
@Override
public void onBackPressed() {
stopSoundAndVibration();
super.onBackPressed();
}
}