我有一個onclick事件按鈕,打開另一個類。另一類正在進行網絡調用以檢索數據,因此如果沒有連接,則應用程序部隊將關閉。檢查數據連接
我該如何實現一個try catch或if語句來檢查數據連接,如果沒有,則顯示吐司這樣說。如果有,然後進入第二課。
我有一個onclick事件按鈕,打開另一個類。另一類正在進行網絡調用以檢索數據,因此如果沒有連接,則應用程序部隊將關閉。檢查數據連接
我該如何實現一個try catch或if語句來檢查數據連接,如果沒有,則顯示吐司這樣說。如果有,然後進入第二課。
我使用:在您的Android清單
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
做這樣的事情,檢查互聯網連接:
static public boolean isInternetActive()
{
ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = (NetworkInfo) connectivity.getActiveNetworkInfo();
if (info == null || !info.isConnected())
{
return false;
}
if (info.isRoaming())
{
return false;
}
return true;
}
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null &&
cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
你也需要。
這段代碼檢查是否是互聯網連接開啓或關閉的代碼檢查wifi和電話網。
public boolean CheckInternet()
{
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Here if condition check for wifi and mobile network is available or not.
// If anyone of them is available or connected then it will return true, otherwise false;
if (wifi.isConnected()) {
return true;
} else if (mobile.isConnected()) {
return true;
}
return false;
}
清單文件::
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
試試這個,這將幫助你..
public class CheckNetworkInfo {
public static boolean haveNetworkConnection(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
Log.v("Yeah", "Internet is working");
// txt_status.setText("Internet is working");
return true;
} else {
// txt_status.setText("Internet Connection Not Present");
Log.v("Sorry", "Internet Connection Not Present");
return false;
}
}
//調用上面類的靜態方法,其中u要檢查網絡可用性
class Myactivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
if(CheckNetworkInfo.haveNetworkConnection(Myactivity.this)){
startActivity(new Intent(HomePage.this,SearchTerm.class));
}
else{
AlertDialog.Builder a=new AlertDialog.Builder(Myactivity.this);
a.setTitle("Check Network Connection");
a.setPositiveButton("OK",new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
a.show();
}
}
}
只需使用它。
從任何地方調用此代碼,它會返回您當前的網絡狀態。
public static boolean isInternetAvailable(Context c)
{
ConnectivityManager connectivityManager
= (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
}
如果連接則返回true,否則返回false。對其輸出採取任何所需的操作。
注意:它是一種靜態方法。因此,如果您只想從Android Activity類中調用它,請移除static關鍵字。