Hello, and I hope you are having fun learning! :)
I took your code and quickly cleaned it up for you. I didn't test it so I
don't know if anything i've done helped. BUT you had a lot of stuff in there
that I would be surprised if it even compiled. I wrote comments explaining
my changes...
Hope it helps
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
// Use something else besides a reserved word for naming your fields,
methods, classes, etc...
// Also you don't need this boolean. You can simply use 'false' or
'true'
// private boolean False;
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main );
mAudioManager = (AudioManager)getSystemService( AUDIO_SERVICE );
checkIfPhoneIsSilent();
// You can wrap your listener with a method however, I find it is
better to init my
// listeners in the onStart method. See the Activity lifecycle
// Example below in the onStart() method...
// setButtonClickListner();
Log.d( "SilentModeApp", "This is a test" );
}
@Override
public void onStart() {
Button toggleButton = (Button)findViewById( R.id.toggleButton );
toggleButton.setOnClickListener( new View.OnClickListener() {
public void onClick( View v ) {
// Do some click stuff here...
}
} );
}
// I have no idea why this is here??? You perform the check in your
checkIfPhoneIsSilent()
// {if (mPhoneIsSilent)
// {
// mAudioManager
// .setRingerMode(AudioManager.RINGER_MODE_NORMAL);
//
// mPhoneIsSilent = False;
// }
// else
// {
// mAudioManager
// .setRingerMode(AudioManager.RINGER_MODE_SILENT);
// boolean True = false;
// mPhoneIsSilent = True;
// }
/**
* Toggles the UI images from silent to normal and vice versa.
*/
void toggleUi() {
ImageView imageView = (ImageView)findViewById( R.id.phone_icon );
Drawable newPhoneImage;
if( mPhoneIsSilent ) {
newPhoneImage = getResources().getDrawable(
R.drawable.phone_silent );
} else {
newPhoneImage = getResources().getDrawable( R.drawable.phone_on
);
}
imageView.setImageDrawable( newPhoneImage );
}
/**
* Checks to see if your phone is currently in silent mode
*
*/
private void checkIfPhoneIsSilent() {
int ringerMode = mAudioManager.getRingerMode();
if( ringerMode == AudioManager.RINGER_MODE_SILENT ) {
mPhoneIsSilent = true;
} else {
mPhoneIsSilent = false;
}
}
protected void onResume() {
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en