在我的项目中,我使用 JSON 解析,在我的第一个活动中,我展示了从 web 服务获取数据的 ListView,问题是当他们没有互联网连接时,它显示空白的 Okey,但是如果我继续进行该活动并且互联网即将到来,那么我希望它我的活动重新加载,该怎么办。
问问题
3421 次
5 回答
2
使用BroadcastReceiver
.
private BroadcastReceiver networkReceiver = new BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getExtras()!=null) {
NetworkInfo ni=(NetworkInfo)intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
// we're connected
}
}
// we're not connected
}
}
在您的onResume()
中注册,然后在onPause()
.
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(networkReceiver);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkReceiver, filter);
}
public boolean isNetworkConnected() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
于 2013-09-02T06:02:09.693 回答
1
以下解决方案适用于我
//MainActivity.java
public class MainActivity extends Activity
{
private ConnectionDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detector = new ConnectionDetector(MainActivity.this);
// check Internet
if (detector.isInternetAvailable())
{
Log.d("===========================", "Internet Present");
}
else
{
Log.d("===========================", "No Internet");
this.registerReceiver(this.mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
@Override
protected void onResume()
{
super.onResume();
Log.d("===========================", "onResume");
IntentFilter intentFilter = new IntentFilter("com.agile.internetdemo.MainActivity");
MainActivity.this.registerReceiver(mConnReceiver, intentFilter);
}
@Override
public void onPause()
{
super.onPause();
Log.d("===========================", "onPause");
MainActivity.this.unregisterReceiver(mConnReceiver);
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if (currentNetworkInfo.isConnected())
{
Log.d("===========================", "Connected");
finish();
startActivity(getIntent());
Toast.makeText(getApplicationContext(), "Connected",Toast.LENGTH_LONG).show();
}
else
{
Log.d("===========================", "Not Connected");
Toast.makeText(getApplicationContext(), "Not Connected",
Toast.LENGTH_LONG).show();
}
}
};
}
//connectionDector.java
public class ConnectionDetector
{
private Context _context;
public ConnectionDetector(Context context)
{
this._context = context;
}
public boolean isInternetAvailable()
{
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
于 2013-09-02T09:56:56.157 回答
0
您可以像这样检查网络连接:
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
还将以下权限添加到 Android Manifest
如果您想在获得连接之前运行它,您可以使用TimerTask
.
于 2013-09-02T05:57:33.590 回答
0
你都可以像递归一样调用函数
public void loadDataFromServer(){
if(isNetworkAvailabel()){
//call web service
}else{
loadDataFromServer();
}
}
于 2013-09-02T06:00:14.357 回答
0
import android.content.Context;
import android.net.ConnectivityManager;
public class DetectConnection {
public static boolean checkInternetConnection(Context context) {
ConnectivityManager con_manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (con_manager.getActiveNetworkInfo() != null
&& con_manager.getActiveNetworkInfo().isAvailable()
&& con_manager.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
}
接着
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (DetectConnection.checkInternetConnection(MainActivity.this)) {
//do something here "your reload Activity"
} else {
Toast.makeText(MainActivity.this,"NO Internet Connection",Toast.LENGTH_LONG).show();
ProgressBar progressBar.setVisibility(View.GONE);
}
于 2018-05-27T07:21:00.957 回答