使用BroadCast Receiver导致结果错误

时间:2011-12-29 13:37:45

标签: java android

我目前正在开发Android中的Web抓取

我有一个代码来检查是否有互联网连接

但如果我转动移动网络,应用程序突然错误想要强行关闭 这里有一些代码:

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);
}

}

我不知道为什么当我关闭移动网络时onReceive方法没有调用

1 个答案:

答案 0 :(得分:3)

要检查是否存在Internet,您可以使用以下代码:

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;
}

如果存在Internet,则此代码返回true,否则返回false。