Reputation: 279
My app needs internet to work so i'm trying to check the internet connection work or not i want when internet is turn off app show the user dialog
with TRY AGAIN button when user click TRY AGAIN Button and interent connection still doesn't work Show Dialog
(internet doesn't work try again )
check internet connection method :
public Boolean CheckInternetConnection(){
ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()) {
return true;
}
else
return false;
}
inside onCreate
protected void onCreate(){
......
......
if(CheckInternetConnection()){
Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.info)
.setTitle("Internet Connection !")
.setMessage("No Internet Connection")
.setNegativeButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(CheckInternetConnection()){
//TRY AGAIN and REOPEN DIALOG
}
}
});
builder.setCancelable(false);
AlertDialog about = builder.create();
about.show();
TextView messageText = (TextView) about.findViewById(android.R.id.message);
assert messageText != null;
messageText.setGravity(Gravity.CENTER);
Button nbutton = about.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setTextColor(Color.BLACK);
}
Upvotes: 0
Views: 109
Reputation: 2094
Create showDialog()
separate method and call it whenever you need to show the dialog again.
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
if (isNetworkConnected()) {
Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show();
} else {
showDialog();
}
}
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("No Internet Connection");
builder.setNegativeButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (isNetworkConnected()) {
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
} else {
showDialog();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Upvotes: 1
Reputation: 4327
if you get the connection fail and then show dialog like "your connection fail" try this code below .
mainactivity
public class MyActivity implements NetworkStateReceiverListener {
private NetworkStateReceiver networkStateReceiver;
}
oncreate
public void onCreate(Bundle savedInstanceState) {
/* ... */
networkStateReceiver = new NetworkStateReceiver();
networkStateReceiver.addListener(this);
this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}
mainactivity
@Override
public void networkAvailable() {
// internet connection success
}
@Override
public void networkUnavailable() {
//REOPEN DIALOG
}
the receiver
public class NetworkStateReceiver extends BroadcastReceiver {
protected List<NetworkStateReceiverListener> listeners;
protected Boolean connected;
public NetworkStateReceiver() {
listeners = new ArrayList<NetworkStateReceiverListener>();
connected = null;
}
public void onReceive(Context context, Intent intent) {
if(intent == null || intent.getExtras() == null)
return;
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();
if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
connected = true;
} else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
connected = false;
}
notifyStateToAll();
}
private void notifyStateToAll() {
for(NetworkStateReceiverListener listener : listeners)
notifyState(listener);
}
private void notifyState(NetworkStateReceiverListener listener) {
if(connected == null || listener == null)
return;
if(connected == true)
listener.networkAvailable();
else
listener.networkUnavailable();
}
public void addListener(NetworkStateReceiverListener l) {
listeners.add(l);
notifyState(l);
}
public void removeListener(NetworkStateReceiverListener l) {
listeners.remove(l);
}
public interface NetworkStateReceiverListener {
public void networkAvailable();
public void networkUnavailable();
}
}
Upvotes: 0