0

我正在尝试编写一个基于时间使电话静音的程序。例如,我按下一个按钮,我可以设置开始时间和结束时间。在那个开始时间,手机会静音。到那时,电话将返回常规振铃模式。目前,它会使手机静音,但一旦到达结束时间就不会回到常规模式。我以前尝试解决此问题的尝试中注释掉了很多东西。未注释的部分是我目前拥有的。我真的很想在这个问题上得到一些帮助/指导。

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

}
4

2 回答 2

0

让这些步骤...

  1. 创建意图服务以使手机静音

    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);
      }
    }
    
  2. 在您的按钮事件中,将手机设置为静音并启动 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);
    }
    

就这样...

注意: 您不需要调用任何构造函数...但是如果您删除带有参数“名称”的构造函数,它将引发异常...所以保持原样...

对于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 转换为 startTime 和 intervalTime 的毫秒...

于 2013-04-05T07:04:37.263 回答
0

阅读服务- 它会服务。问题在于当时不存在aktiviti,如何开启声音。

于 2013-04-05T06:11:43.893 回答