Reputation: 147
I have an activity1 who checks an internet connection, if the user doesn't have an internet conection, the app sends him to Wireless Settings.
I wanna know how to check if the user has activated the internet data when he get back to the activity1 which called the wirelss settings.
I hope your HELP. Thank you.
public void myThread(){
Thread th=new Thread(){
@Override
public void run(){
splash_screen.this.runOnUiThread(new Runnable() {
public void run() {
dialog.conectarRed().show();
}
});
}
};
th.start();
}
}
public Dialog conectarRed() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
return builder
.setTitle("No hay Conexi�n a Internet")
.setMessage("� Desea activar la Conexi�n de Datos ?")
.setCancelable(false)
.setPositiveButton("Aceptar",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
((Activity) context).startActivity(i);
dialog.cancel();
}
})
.setNegativeButton("Cancelar",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
((Activity) context).finish();
dialog.cancel();
}
}).create();
}
Upvotes: 1
Views: 52
Reputation: 500
Check for connectivity in onStart()
. You can determine whether you have wifi or moble connection active. If not start wireless settings activity, when you come back you will find out if the user has activated either.
@Override
protected void onStart() {
super.onStart();
if (!CheckConnectivity()){
Toast.makeText(this, R.string.toast_enable_connectivity,Toast.LENGTH_SHORT).show();
Handler launchHandler = new Handler();
Log.d(TAG,"launching EnableConnectivity()");
launchHandler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
}, 2000);
}
}
private boolean CheckConnectivity(){
NetworkInfo networkInfo;
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean connectivityAvailable = false;
if (connManager == null){
Toast.makeText(this, R.string.toast_conn_no_service ,Toast.LENGTH_SHORT).show();
} else {
networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
connectivityAvailable |= networkInfo.isConnected();
networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
connectivityAvailable |= networkInfo.isConnected();
}
return connectivityAvailable;
}
Upvotes: 1
Reputation: 101
You should use this
((Activity) context).startActivityForResult(i, 0);
then you can use onActivityResult in your Activity1
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Check result
}
Upvotes: 0