i'm very new to android, and basically modifying an app i find on
textbook. This app contain a single ImageButton on the center of the
screen. it will detect the ringer mode of the phone, and when clicked,
it will change the ringer mode from silent to normal or normal to
silent.

i declare ImageButton twice. Once in each procedure. If i declare
ImageButton as global variable just like the variable silent and
ringer, the app will fail. can somebody tell me why this is?

private AudioManager ringer;
private boolean silent;
ImageButton imgBtn=(ImageButton) findViewById(R.id.imgBtn);

this is the working version of the app.

public class MainActivity extends Activity {

    private AudioManager ringer;
    private boolean silent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ringer=(AudioManager) getSystemService(AUDIO_SERVICE);
        checkPhoneStatus();
        setButtonListener();
    }

    private void checkPhoneStatus(){
        int ringing=ringer.getRingerMode();
        if(ringing==AudioManager.RINGER_MODE_NORMAL)
                silent = false;
        else
                silent=true;
    }
    private void setButtonListener(){
        ImageButton imgBtn=(ImageButton) findViewById(R.id.imgBtn);
        imgBtn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                if(silent){
                                        
ringer.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                                        silent=false;
                                }else{
                                        
ringer.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                                        silent=true;
                                }
                                changeButton();
                        }
                });
    }
    private void changeButton(){
    ImageButton imgBtn=(ImageButton) findViewById(R.id.imgBtn);
        if(silent)
        
imgBtn.setImageDrawable(getResources().getDrawable(R.drawable.phone_silent));
        else
 
imgBtn.setImageDrawable(getResources().getDrawable(R.drawable.phone_on));

    }
    @Override
        protected void onResume(){
                super.onResume();
                checkPhoneStatus();
                changeButton();
        }
}

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

Reply via email to