Theo Theo - 3 months ago 11
Android Question

Checking if there is internet connection

I would like to ask something that I don't understand in my app. I get some data from my server and display them in a recyclerview using the google's Volley library. So far so good:).

Next I get those data from the list and add them to SQLite via a content provider. And finally if there is no internet connection,I should read them from the phone's repository(for now I should get a Toast message stating that there is no internet connection). Here is the thing. When I turn off the wifi the NoInternet Activity doesn't launch. However when I put my phone in Airplane mode the NoInternet Activity does launch. Here is my code.

public class AnnouncementsFragment extends Fragment {
public String titleForContentProvider;
public String imageForContentProvider;
public String articleForContentProvider;
public static final String TAG = "AelApp";
private ArrayList<MyModel> listItemsList;
private static final String IMAGE_URL = "";
RecyclerView myList;
private AnnouncementsAdapter adapter;

public AnnouncementsFragment() {
// Required empty public constructor

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

View rootView = inflater.inflate(R.layout.fragment_announcements, container, false);
listItemsList = new ArrayList<>();

myList = (RecyclerView) rootView.findViewById(;
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
adapter = new AnnouncementsAdapter(getActivity(), listItemsList);

if (isOnline()) {
Intent i = new Intent(getActivity(), NoInternet.class);

return rootView;
public void updateAnnouncementsList() {

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(getActivity());

// Request a string response from the provided URL.
JsonArrayRequest jsObjRequest = new JsonArrayRequest(Request.Method.GET, URL.GET_ANNOUNCEMENTS, new Response.Listener<JSONArray>() {

public void onResponse(JSONArray response) {

Log.d(TAG, response.toString());

// Parse json data.
// Declare the json objects that we need and then for loop through the children array.
// Do the json parse in a try catch block to catch the exceptions
try {

for (int i = 0; i < response.length(); i++) {

JSONObject post = response.getJSONObject(i);

MyModel item = new MyModel();
item.setImage(IMAGE_URL + post.getString("announcement_image"));

//Getting the string values out of the JSON response.
titleForContentProvider = post.getString("title");
imageForContentProvider = post.getString("announcement_image");
articleForContentProvider = post.getString("article");
//I added them as a key value pair.
ContentValues values = new ContentValues();
//A Content Resolver that allows the app to
//to insert data to the database after
//using the Uri defined in the Content Provider
Uri uri = getActivity().getContentResolver().insert(AELProvider.CONTENT_URL, values);
Toast.makeText(getContext(), "Announcement added", Toast.LENGTH_LONG)

} catch (JSONException e) {

// Update list by notifying the adapter of changes
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());


protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;

Any ideas?




You can try this:

public class Common extends BroadcastReceiver
    public static boolean internet_status = false;
    public static void checkInternetConenction(Context context) {
        internet_status = false;
        ConnectivityManager check = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (check != null) {
            NetworkInfo[] info = check.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        internet_status = true;

    public void onReceive(Context context, Intent intent)

And Add receiver in Android Menifest file:

<receiver android:name=".Common" >
                <action android:name="" >

Set boolean value to false inside your activity:

static boolean internet_status = false;

& check connection using:

                        //Do your stuff here
                        Toast.makeText(YourActivity.this, "Internet connection is not available", Toast.LENGTH_LONG).show();