来看看我让讯飞心火给我写的安卓语音报时软件代码

我给的他要求是无障碍必须良好,声音和震动可以自定义。以下是一个简单的安卓语音报时软件的代码示例:


 


import android.app.AlarmManager;


import android.app.PendingIntent;


import android.content.Context;


import android.content.Intent;


import android.media.Ringtone;


import android.media.RingtoneManager;


import android.net.Uri;


import android.os.Build;


import android.os.Bundle;


import android.os.Vibrator;


import android.speech.tts.TextToSpeech;


import android.support.v7.app.AppCompatActivity;


import android.view.View;


import android.widget.Button;


import android.widget.TimePicker;


import java.util.Calendar;


public class MainActivity extends AppCompatActivity {


private TimePicker timePicker;


private Button setAlarmButton;


private TextToSpeech textToSpeech;


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);


timePicker = findViewById(R.id.timePicker);


setAlarmButton = findViewById(R.id.setAlarmButton);


textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {


@Override


public void onInit(int status) {


if (status != TextToSpeech.ERROR) {


textToSpeech.setLanguage(Locale.CHINESE);


}


}


});


setAlarmButton.setOnClickListener(new View.OnClickListener() {


@Override


public void onClick(View v) {


setAlarm();


}


});


}


private void setAlarm() {


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);


AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);


Intent intent = new Intent(this, AlarmReceiver.class);


PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);


} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {


alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);


} else {


alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);


}


}


public class AlarmReceiver extends BroadcastReceiver {


@Override


public void onReceive(Context context, Intent intent) {


String message = "时间到了!";


textToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null, null);


Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);


Ringtone r = RingtoneManager.getRingtone(context, notification);


r.play();


Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);


vibrator.vibrate(2000);


}


}


}


 


这个代码示例包括一个主界面,用户可以在其中设置闹钟的时间。当闹钟响起时,会通过TextToSpeech播报预设的消息,同时播放预设的铃声并震动设备。你可以根据需要自定义消息、铃声和震动模式。

发表于:2024-03-15 10:03
7个回复
您还没有登录,登录后才可回复。 登录 注册