0

I am currently developing Web Crawling in android

I have a code to check whether there is internet connection or not

but if i turned the mobile network the application suddenly error wanted to force close here are some code :

package thesis.carlo;

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub

    receiver = new ConnectivityReceiver();
    registerReceiver(receiver, new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION));



    Notification note = new Notification(R.drawable.target, "Crawling", System.currentTimeMillis());
    Intent i = new Intent(this, LoadingActivity.class);

    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

    note.setLatestEventInfo(this, "Focused Crawling","In Progress Crawling...", pi);
    note.flags |= Notification.FLAG_NO_CLEAR;

    startForeground(1337, note);


    this.crawls(resultMap, keywords);

    stopForeground (true);

    this.showResult(urlsList, valueList);
    result = Activity.RESULT_OK;


}


public void showResult(List urlList, List valList) {

    Intent i = new Intent(this, ResultActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putStringArrayListExtra("urlsList", (ArrayList<String>) urlList);
    i.putParcelableArrayListExtra("valueList",
            (ArrayList<? extends Parcelable>) valList);
    startActivity(i);
}

private String getNetworkStateString(NetworkInfo.State state) {
    String stateString = "Unknown";

    switch (state) {
    case CONNECTED:
        stateString = "Connected";
        break;
    case CONNECTING:
        stateString = "Connecting";
        break;
    case DISCONNECTED:
        stateString = "Disconnected";
        break;
    case DISCONNECTING:
        stateString = "Disconnecting";
        break;
    case SUSPENDED:
        stateString = "Suspended";
        break;
    default:
        stateString = "Unknown";
        break;
    }

    return stateString;
}

public void onDestroy() {
    unregisterReceiver(receiver);

    super.onDestroy();
}

private class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NetworkInfo info = intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

        if (null != info) {
            String state = getNetworkStateString(info.getState());
            String stateString = info.toString().replace(',', '\n');

            Log.i("ConnTest", info.getTypeName());
            Log.i("ConnTest", state);
            Log.i("ConnTest", info.toString());

            if(state.equals("Disconnected")){
                stop();
            }

        }
    }
}

public void stop(){
    stopService(new Intent(this, CrawlingActivityService.class));
    startActivity(new Intent(this, ErrorActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    stopForeground (true);
}

}

I dont know why the onReceive method didn't invoke when I turned of the mobile network

Carlo
  • 1,147
  • 3
  • 15
  • 23

1 Answers1

3

To check whether Internet is there or not you can use the following code:

public static Boolean isOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

This code returns true if Internet is present and false if it is not.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Rishi
  • 987
  • 6
  • 16
  • Are the above code can be used during the application for example : first I open the application and then during the middle process I turned the mobile network down . will the above code invoke during the service process – Carlo Dec 29 '11 at 13:46
  • This code are worked when first time you check whether the internet is present or not. It is not work well during the service because every time you need to check whether internet is present or not... – Rishi Dec 29 '11 at 13:54
  • so do you know how to check the internet during the service – Carlo Dec 29 '11 at 13:56
  • You just go through this link http://stackoverflow.com/questions/3141807/android-service-to-check-internet-connectivity – Rishi Dec 29 '11 at 14:00