I used a Broadcast Receiver for listening the current wifi state. So it sets the current state to the text (connected, connecting, disabled,...) of a togglebutton (setText).
It works fine!
But now I want to do the same thing with the mobile data state..
So I used TelephonyManager to setup the receiver:
this.registerReceiver(this.DataStateChangedReceiver,
new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
Then I copied the code from the wifi receiver and edited it:
private BroadcastReceiver DataStateChangedReceiver
= new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
int extraDataState = intent.getIntExtra(TelephonyManager.EXTRA_STATE ,
TelephonyManager.DATA_DISCONNECTED);
switch(extraDataState){
case TelephonyManager.DATA_CONNECTED:
data_toggle.setChecked(true);
break;
case TelephonyManager.DATA_DISCONNECTED:
data_toggle.setChecked(false);
break;
case TelephonyManager.DATA_CONNECTING:
data_toggle.setChecked(true);
break;
case TelephonyManager.DATA_SUSPENDED:
data_toggle.setChecked(true);
break;
}
}
};
The app starts but nothing happened with the toogleButton.. Is TelephonyManager the wrong way to do this? ConnectivityManager?
I want to set an onclicklistener to turn on / off the mobile data.
How to do this is the next question..