Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unfortunately app is getting stopped while checking for network

I am using the following code to check for the internet connection through out my app.

public class ConnectionChangeReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
  {
    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );

    if (activeNetInfo != null)
    {
        Toast.makeText( context, "Active Network Type : " + 
                       activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
    if(mobNetInfo != null)
    {
        Toast.makeText( context, "Mobile Network Type : " + 
                          mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
  }
}

And I have defined required permission in the manifest file.

Whenever I try to disconnect / connect the network using F8 key I will receive "UNFORTUNATELY APP HAS STOPPED", and I am not getting any print in logcat.

Can I know what is the mistake I am doing?

like image 685
Beginner Avatar asked Mar 21 '13 11:03

Beginner


People also ask

Why is my phone saying Unfortunately app has stopped?

The “Unfortunately, App has stopped” message can also be caused by a corrupted SD card. To check, remove the SD card and try the app again. If it works this time, the card is the issue. You need a new memory card, but you can transfer the data from that card to your computer, then later add it to the new card.

How do you fix a network error on an app?

If your app displays a network error message, try the following: Turn the Wi-Fi off by tapping Settings > Wi-Fi > Off. Turn off Airplane Mode by tapping Settings > Airplane Mode > Off. Turn Cellular Data on by tapping Settings App > Wireless & Networks (header) > More… > Mobile Networks > Data Enabled.


1 Answers

NOTE: If you're targeting android N Preview. Above receiver will not work as per constrained restricted by Google.

Link: https://developer.android.com/preview/features/background-optimization.html#connectivity-action

Use: WorkManager or JobScheduler for same.


Have you added this in AndroidManifest.xml?

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

if you have added above then check this code:

<receiver android:name=".UpdateReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

and

public class UpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

          ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE );
          NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
          boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();   
          if (isConnected)       
              Log.i("NET", "Connected" + isConnected);   
          else 
              Log.i("NET", "Not Connected" + isConnected);
    }
}

like image 95
Dhaval Parmar Avatar answered Sep 21 '22 01:09

Dhaval Parmar