我正在尝试编写一个程序,根据时间让手机静音。所以,例如,我按一个按钮,我可以设置开始时间和结束时间。在那个开始时间,手机会静音。在那个结束时,手机将返回常规铃声模式。目前它会使手机静音,但一旦达到结束时间就不会恢复到常规模式。从以前试图解决这个问题的尝试中我注释了很多东西。未注释的部分是我目前拥有的。我真的希望得到一些关于这个问题的帮助/指导。
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;
}
}
答案 0 :(得分:0)
了解Service - 它会投放。问题在于当时不存在aktiviti,如何打开声音。
答案 1 :(得分:0)
让我们执行这些步骤......
创建意向服务以取消静音手机
public class SetToNormalService extends IntentService {
public SetToNormalService(String name) { super(name); }
public SetToNomralService() { super(null); }
@Override
protected void onHandleIntent(Intent intent) {
// set the phone ringer to normal
mAudioManager =(AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
在按钮事件中,将手机设置为静音并启动AlarmManager
public void SilentAndSetAlarm(Context context,long triggerAtMillis, long intervalMillis) {
// set the phone to silent
mAudioManager =(AudioManager)PresetEdit.this.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
// Initialize the AlarmManager
Intent intent = new Intent(context, SetToNomralService.class);
PendingIntent pIntent = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, intervalMillis, pIntent);
}
多数人......
备注:强> 你不需要调用任何构造函数......但是如果删除带有参数'name'的那个,它将抛出一个异常......所以保持原样......
对于millis ......你可以使用
//get current time object
Date date = new Date();
// set its hours and mimuts that you got from time picker
date.setHours(hoursOfDay);
date.setMinutes(minutesOfDay);
//getTime will give you millisecond of date object
long timeInMillis = date.getTime();
以这种方式...你可以将你的eHours和eMinutes转换为millis,startTime和intervalTime ......