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