来看看我让讯飞心火写的报时软件代码

9 大诚 8个月前 424次点击

首先我给他的要求是写一款语音报时的安卓软件,无障碍良好,可以自定义声音和震动。

以下是一个简单的安卓语音报时软件的代码示例:

 

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播报预设的消息,同时播放预设的铃声并震动设备。你可以根据需要自定义消息、铃声和震动模式。

有没有懂编程的朋友,可以用安卓的编程软件给她打包,看看效果如何?

共 8 条评论
流星 8个月前
0 

这个我一点都不懂。

曼陀罗 8个月前
0 

听天书。

凌川独步 8个月前
0 

天呐。这代码写这么详细的,我以为顶多就是个片段

绝对运行不起来。

0 

哎,看不懂。

月落星沉 8个月前
0 

为什么第一个导入的前面没有这个?

require

Import

月落星沉 8个月前
0 

你们可以试一下这个能不能用。

Require

import

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.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() {

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

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)

月落星沉 8个月前
0 

记住这里面双引号下的我没有加,你们得自己去看在后面的。需要导入的名字那里填双引号,还有点号,还有星。

添加一条新评论

登录后可以发表评论 去登录