开发者

How to reboot my current activity,when i connect net?

I am developing an application and I am displaying images from URl using xml parsing. Wh开发者_Go百科en internet gets disconnected I pop up alert window.

Now I wan my current activity automatically reboot, when my device connect net.


You need to have a BroadcastReceiver to listen to internet changes. If you just need this logic in your activity you can do something like this:

private BroadcastReceiver mReceiver;
private IntentFilter mFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {

  mFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
  mReceiver = new ConnectionChangeReceiver();
}

@Override
protected void onResume() {
  super.onResume();
  registerReceiver(mReceiver, mFilter);
}

@Override
protected void onPause() {
  super.onPause();
  unregisterReceiver(mReceiver);
}


private class ConnectionChangeReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context arg0, Intent i) {
    ConnectivityManager connectivityManager = (ConnectivityManager) ActivityActionBar.this
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager.getActiveNetworkInfo() == null) {
      onLoseInternet();
    }
}
}

onLoseInternet() method you should do something to reset everything. Possibilities:

Best way I found so far

In the onCreate() method I have two calls: getWidgets() and populateWidgets().

getWidgets() is in charge of doing all the findViewById stuff and populateWidgets() will basically populate them with the correct info.

So when I need to reset the info I just call populateWidgets().

Hacky way

I don't know if this will work but you can try doing:

  • finish();
  • startActivity(new Intent(this, this.class));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜