I'm trying to create an app which sets ringer mode to silent from vibrate or normal mode, I checked the code much time but not able to find the problem in my code. Hope community will find the problem. And Help me to resolve.
public class MainActivity extends AppCompatActivity {
private AudioManager myAudioManager;
private Button silentBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
silentBtn = findViewById(R.id.button);
myAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
silentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mod = myAudioManager.getRingerMode();
if (mod == AudioManager.RINGER_MODE_SILENT) {
Toast toast = Toast.makeText(MainActivity.this, "You have wasted your time by clicking this Button.", Toast.LENGTH_SHORT);
toast.show();
} else {
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast toast1 = Toast.makeText(MainActivity.this, "You are Good to Go.", Toast.LENGTH_SHORT);
toast1.show();
}
}
});
}
}
I expect that the app should not crash when I click the button.