2

I have 2 buttons in my app starttime and endtime

Now I want the device to turn in silent mode during the start and end time duration set by the user.....how can I do this?

My code for taking input for starttime and endtime mainactivity.java code package com.example.h.manualsilent;

public class MainActivity extends AppCompatActivity {
TimePickerDialog tpd;
SimpleDateFormat simpleDateFormat;
String time;
Button sttime;
Button entime;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sttime=(Button)findViewById(R.id.startbtn);
    entime=(Button)findViewById(R.id.endbtn);
}
public void starttime(View view){
    Calendar cal=Calendar.getInstance();
    simpleDateFormat=new SimpleDateFormat("hh:mm a");
    int hour=cal.get(Calendar.HOUR);
    int minute=cal.get(Calendar.MINUTE);

    //int inst=cal.get(Calendar.AM_PM);
    tpd=new TimePickerDialog(MainActivity.this, new 
    TimePickerDialog.OnTimeSetListener() {

        @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Time time = new Time(hourOfDay, minute,0);

            //little h uses 12 hour format and big H uses 24 hour format
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");

            //format takes in a Date, and Time is a sublcass of Date
            String s = simpleDateFormat.format(time);
            sttime.setText(s);
        }
    },hour,minute,false);
    tpd.show();
    }
    public void endtime(View view){
    Calendar cal=Calendar.getInstance();
    int hour=cal.get(Calendar.HOUR);
    int minute=cal.get(Calendar.MINUTE);
    //int inst=cal.get(Calendar.AM_PM);
    tpd=new TimePickerDialog(MainActivity.this, new 
    TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Time time = new Time(hourOfDay, minute,0);

            //little h uses 12 hour format and big H uses 24 hour format
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");

            //format takes in a Date, and Time is a sublcass of Date
            String s = simpleDateFormat.format(time);
            entime.setText(s);
        }
        },hour,minute,false);
        tpd.show();
        }
        }
Zain Ul Abidin
  • 109
  • 2
  • 11

2 Answers2

2

You can use AudioManager for changing from genral mode to silent mode.

Code:

AudioManager audioManager;
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// changing to silent mode
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

You have to give the required permission also in manifest file.

android.permission.MODIFY_AUDIO_SETTINGS

You can use the above code to set the phone to silent based on the condition that you need.

You can also change back to VIBRATE or GENERAL using AudioManager.RINGER_MODE_VIBRATE or AudioManager.RINGER_MODE_NORMAL respectively.

Community
  • 1
  • 1
Johny
  • 2,128
  • 3
  • 20
  • 33
  • ok now I know how to toggle between silent and general mode...this is helpful....but how can I keep tracking the duration set by user and activate silent or general mode....I have edited my question...can you help @Johny – Zain Ul Abidin Aug 13 '17 at 10:55
2

You can use the AudioManager class.

In this class you're looking for setRingerMode() function.

AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);

The values you can pass into the function are:

The ringer mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT, or RINGER_MODE_VIBRATE.

You have to add this into the manifest file:

android.permission.MODIFY_AUDIO_SETTINGS

I saw this here - https://stackoverflow.com/a/3738768/8214839

Stacy Chen
  • 221
  • 1
  • 5