2

Hi i am working on an application that need to turn on/off the gprs via code. I am using this methord to check if connection is On/Off. But i have no idea how to turn on gprs if it is off

code

NetworkInfo i = conMgr.getActiveNetworkInfo();
  if (i == null)
    return false;
  if (!i.isConnected())
    return false;
  if (!i.isAvailable())
    return false;
  return true;
Akshay
  • 2,506
  • 4
  • 34
  • 55
Sumit
  • 928
  • 2
  • 15
  • 34

1 Answers1

2

Use this code to Broadcast Wifi or GPRS availability

BroadcastReceiver br;
br = new BroadcastReceiver() {

 @Override
public void onReceive(Context arg0, Intent intent) {
    // TODO Auto-generated method stub  
        String action = intent.getAction();
        if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
           {
              return;
           }
  boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 
  NetworkInfo aNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);                            

      if (!noConnectivity)
         {
          if (aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) 
               {
                    Toast.makeText(getApplicationContext(), "Mobile Connected", Toast.LENGTH_LONG).show();
                      //Handle connected case
               }
         if (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)
               {
                    Toast.makeText(getApplicationContext(), "WIFI Connected", Toast.LENGTH_LONG).show();
               }    
          }

       else
         {
              if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) ||
                            (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
                    {
                        Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
                    }
         }              
     }
};

Provide following Permissions in androidmanifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And don't forget to register and unregister BroadcastReceiver.

Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
  • -Rstar This will only display whether Gprs is connected or not what is i have to on gprs via code through mobile network not via wi-fi – Sumit Jul 28 '12 at 09:00
  • buddy how is it possible ? coz to enable GPRS we must have knowledge of APN point ,IP etc. – Ronak Mehta Jul 28 '12 at 09:01
  • I don't know how its possible that why i asked the question – Sumit Jul 28 '12 at 09:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14566/discussion-between-sumit-and-rstar) – Sumit Jul 28 '12 at 12:45