tags:

views:

33

answers:

1

Hi all,

I am new to Android development and have trouble enabling / disabling the wifi & audio services. I get the appropriate manager instance using the getSystemService method. But I don't get any error when enabling wifi using:

wifiMgr.setWifiEnabled(true);           

But the wifi is simply not turned on! Similarly I use

mAudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
or
mAudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

But the phone doesn't go to silent mode! Here is my complete code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try{            

    final ToggleButton bt = (ToggleButton)findViewById(R.id.bluetooth_button);
    final ToggleButton wf = (ToggleButton)findViewById(R.id.wifi_button);
    final ToggleButton sb = (ToggleButton)findViewById(R.id.sound_button);

    // Check Audio
    AudioManager mAudio = (AudioManager) getSystemService(Activity.AUDIO_SERVICE);
    soundStatus = mAudio.getRingerMode();
    if(soundStatus == AudioManager.RINGER_MODE_NORMAL)
        sb.setChecked(true);      

    // Check WiFi
    WifiManager wifiMgr =   (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiStatus = wifiMgr.isWifiEnabled();
    if(wifiStatus)
        wf.setChecked(true);      

    sb.setOnClickListener(new OnClickListener() {           
        public void onClick(View v) {
            try {
                TextView tv = (TextView) findViewById(R.id.result); 
                AudioManager mAudio = (AudioManager) getSystemService(Activity.AUDIO_SERVICE);
                if(sb.isChecked()) {
                    mAudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    tv.setText("Ringer set to Normal");
                } else {
                    mAudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    tv.setText("Ringer set to Silent");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

    bt.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            try {
                TextView tv = (TextView) findViewById(R.id.result);             
                if(bt.isChecked())
                    tv.setText("Bluetooth is On!");
                else
                    tv.setText("Bluetooth is Off!");
            } catch (Exception e) {

                e.printStackTrace();
            }

        }
    });

    wf.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            try {

                TextView tv = (TextView) findViewById(R.id.result);
                WifiManager wifiMgr =   (WifiManager) getSystemService(Context.WIFI_SERVICE);
                if(wf.isChecked()) {
                    wifiMgr.setWifiEnabled(true);                       
                    tv.setText("Wifi is On!");                      
                } else {
                    wifiMgr.setWifiEnabled(false);
                    tv.setText("Wifi is Off!");                     
                }
            } catch (Exception e) {

                e.printStackTrace();
            }

        }
    });

    }catch (Exception e) {
        e.printStackTrace();
    }
}

I have added the following permissions:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

I am using Motorola milestone (android 2.1).. What Am I missing? can anybody point me what is wrong with this code?
Thanks in Advance for suggestions and corrections.

A: 

The Issue was solved by added more permissions! For people who are facing the same issue, you will have to add the following permissions as well:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

Now, In addition to these I needed Audio and Bluetooth access. for that I had to add:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
Abdel Olakara