我正在尝试编写一个基于时间使电话静音的程序。例如,我按下一个按钮,我可以设置开始时间和结束时间。在那个开始时间,手机会静音。到那时,电话将返回常规振铃模式。目前,它会使手机静音,但一旦到达结束时间就不会回到常规模式。我以前尝试解决此问题的尝试中注释掉了很多东西。未注释的部分是我目前拥有的。我真的很想在这个问题上得到一些帮助/指导。
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class PresetEdit extends Activity
{
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
Button save;
private Button startTime;
private Button endTime;
private int sHour;
private int sMinute;
private int eHour;
private int eMinute;
static final int TIME_DIALOG_ID = 0;
static final int TIME_DIALOG_ID2 = 1;
private TimePickerDialog.OnTimeSetListener sTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
sHour = hourOfDay;
sMinute = minute;
updateDisplay();
displayToast();
}
};
private TimePickerDialog.OnTimeSetListener eTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
eHour = hourOfDay;
eMinute = minute;
updateDisplayE();
displayToastE();
}
};
private void updateDisplay() {
startTime.setText(
new StringBuilder()
.append(pad(sHour)).append(":")
.append(pad(sMinute)));
}
private void updateDisplayE(){
endTime.setText(new StringBuilder()
.append(pad(eHour)).append(":")
.append(pad(eMinute)));
}
private void displayToast() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(startTime.getText()), Toast.LENGTH_SHORT).show();
}
private void displayToastE() {
Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(endTime.getText()), Toast.LENGTH_SHORT).show();
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.preset_edit);
TextView eventTxt = (TextView)findViewById(R.id.txtViewEvent);
startTime = (Button)findViewById(R.id.btnStartTime);
endTime = (Button)findViewById(R.id.btnEndTime);
startTime.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
showDialog(TIME_DIALOG_ID);
}
});
endTime.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
showDialog(TIME_DIALOG_ID2);
}
});
final Calendar cal = Calendar.getInstance();
sHour = cal.get(Calendar.HOUR);
sMinute = cal.get(Calendar.MINUTE);
updateDisplay();
final Calendar calE = Calendar.getInstance();
Calendar tmp = (Calendar)calE.clone();
tmp.add(Calendar.HOUR, 0);
tmp.add(Calendar.MINUTE, 2);
//eHour = calE.get(Calendar.HOUR);
Calendar stopTime = tmp;
//eMinute = calE.get(Calendar.MINUTE);
eHour = tmp.get(Calendar.HOUR);
eMinute = tmp.get(Calendar.MINUTE);
updateDisplayE();
//save = (Button)findViewById(R.id.btnSave);
//get event txt to change according to button click
/*Bundle car = getIntent().getExtras();
String receivingdata = car.getString("eventCar");
TextView eventTxt = (TextView)findViewById(R.id.event);
eventTxt.setText(receivingdata);*/
/*Intent a = getIntent();
a.getStringExtra("eventDrive");*/
String drv = getIntent().getExtras().getString("event");
if(drv!=null)
{
eventTxt.setText(drv);
Time now = new Time(Time.getCurrentTimezone());
//Time end = new Time(Time.getCurrentTimezone());
Time current = new Time(Time.getCurrentTimezone());
//Time setToEnd = new Time(Time.getCurrentTimezone());
current.setToNow();
now.setToNow();
//now.hour = 6;
//now.minute = 35;
//String sTime = ""+ now.hour+":"+now.minute;
//startTime.setText(sTime);
//end.hour = now.hour;
//end.minute = now.minute + 2;
//String eTime = ""+ end.hour+":"+end.minute;
//endTime.setText(eTime);
//setToEnd.hour = eHour;
//setToEnd.minute = 13;
if (sHour == now.hour && sMinute == now.minute)
{
mAudioManager =(AudioManager)PresetEdit.this.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//or adjust volume here instead setting silent profile for the ringer
}
if(current.hour == eHour && current.minute == eMinute)
{
mAudioManager =(AudioManager)PresetEdit.this.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}//end if
String mov = getIntent().getExtras().getString("event");
if(mov!=null)
eventTxt.setText(mov);
String study = getIntent().getExtras().getString("event");
if(study!=null)
eventTxt.setText(study);
String act = getIntent().getExtras().getString("event");
if(act!=null)
eventTxt.setText(act);
/*Intent intent = getIntent();
String text = intent.getStringExtra("eventDriving");*/
//eventTxt.setText(text);
}
/*private void setButtonClickListener() {
save = (Button)findViewById(R.id.btnSave);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mPhoneIsSilent) {
//change back to normal mode
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
save.setText("Activate");
}
else
{
//change to silent mode
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
mPhoneIsSilent = true;
save.setText("Activated");
}
// Now toggle the UI again
}
});
}
private void checkIfPhoneIsSilent() {
int ringermode = mAudioManager.getRingerMode();
if (ringermode == AudioManager.RINGER_MODE_SILENT) {
mPhoneIsSilent = true;
}
else
{
mPhoneIsSilent = false;
}
}
@Override
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
}*/
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
sTimeSetListener, sHour, sMinute, false);
case TIME_DIALOG_ID2:
return new TimePickerDialog(this,
eTimeSetListener, eHour, eMinute, true);
}
return null;
}
}