0

In my app I'm checking whether internet is connection is available or not. I'm using broadcast receiver for this. When I run my app,(when internet is connected) it works. But when I disconnect the internet, it get crashed. What is happening. Here is my code: My activity:

public class BroadcastActivity extends ActionBarActivity {
  private static Button mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);
        Intent intent=new Intent(this, ConnectionReciever.class);
        sendBroadcast(intent);

    }


}

Receiver class:

public class ConnectionReciever extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo=connectivityManager.getActiveNetworkInfo();
        if(activeNetInfo!=null & activeNetInfo.isConnected())
        {
            Toast.makeText(context, "Internet Connection is Active", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(context, "Internet Connection Timed Out! Please Try Again!!", Toast.LENGTH_LONG).show();
        }

    }

}

Also i added the permissions in the manifest and registered the receiver in the manifest.

Log cat:

    Failed to install Check_Network_Status.apk on device 'emulator-5554': adb rejected install command with: device offline
    [2014-05-15 12:09:48 - Check_Network_Status] com.android.ddmlib.AdbCommandRejectedException: device offline
    [2014-05-15 12:09:48 - Check_Network_Status] Launch canceled!
user3619298
  • 17
  • 1
  • 1
  • 7

2 Answers2

0

replace the following code in receiver class, u need to check for one more condition isAvailable() inside if.

ConnectivityManager connectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo=connectivityManager.getActiveNetworkInfo();
    if(activeNetInfo!=null && activeNetInfo.isAvailable() && activeNetInfo.isConnected())
    {
        Toast.makeText(context, "Internet Connection is Active", Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(context, "Internet Connection Timed Out! Please Try Again!!", Toast.LENGTH_LONG).show();
    }
Shrikant
  • 1,560
  • 1
  • 15
  • 32
  • I have one query. Can I use sendBroadcast(), while registering the receivers statically. Is there any other way to send the broadcasts? – user3619298 May 15 '14 at 08:54
  • AFAIK yes you can dynamically resister un-register a receiver, u can check this link to do so. http://stackoverflow.com/questions/8241128/dynamically-register-unregister-a-broadcast-receiver-in-android – Shrikant May 15 '14 at 11:34
  • That means sendBroadcast() has to be used, if we are registering the broadcast reciever in the manifest? – user3619298 May 15 '14 at 11:48
0

Exit eclipse, kill adb and restart eclipse. Try adb kill-server and adb start-server to restart the adb. It can be found in your android sdk directory, in the subfolder tools.

kgandroid
  • 5,507
  • 5
  • 39
  • 69