Android Check Network Status
public void CheckStatus() {
ConnectivityManager connectionstate = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = connectionstate.getActiveNetworkInfo();
if (network != null && network.isConnected() == true) {
Toast.makeText(this, "Network Available", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Network Not Available", Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(ChatsActivity.this)
.setTitle("Connection failed")
.setMessage("This application requires network access. Please, enable " + "mobile network or Wi-Fi.")
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Intent intent = new Intent(MainActivity.this, Settings.ACTION_WIRELESS_SETTINGS);
// startActivity(intent);
ChatsActivity.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ChatsActivity.this.finish();
}
})
.show();
}
}
and also add these permissions in manifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
No comments:
Post a Comment