我想处理这个事件,它将检查是否有互联网连接,如果是,我会调用这段代码:
/** Called when the user clicks the ImageView */
public void mapOfWasteActivity() {
mapOfWaste = (ImageView)findViewById(R.id.imageViewMap);
mapOfWaste.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Main_Activity.this, MapOfWasteActivity.class);
startActivity(intent);
}
});
}
如果你没有与此有联系,请致使我:
Intent intent = new Intent(Main_Activity.this, Test.class);
startActivity(intent);
答案 0 :(得分:4)
使用此功能。如果连接或连接,它将返回true。
public boolean isNetworkConnected() {
boolean isConnected=false;
try{
ConnectivityManager cm =
(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
return isConnected;
}
catch(Exception e)
{
e.printStackTrace();
}
return isConnected;
}
答案 1 :(得分:0)
试试此代码
/** Called when the user clicks the ImageView */
public void mapOfWasteActivity() {
mapOfWaste = (ImageView)findViewById(R.id.imageViewMap);
mapOfWaste.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isNetworkConnected()){
Intent intent = new Intent(Main_Activity.this, MapOfWasteActivity.class);
startActivity(intent);
}else{
Intent intent = new Intent(Main_Activity.this, Test.class);
startActivity(intent);
}
}
});
}
public boolean isNetworkConnected() {
boolean isConnected=false;
try{
ConnectivityManager cm =
(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
return isConnected;
}
catch(Exception e)
{
e.printStackTrace();
}
return isConnected;
}