Reputation: 13
I wanna display a progress bar while checking server connectivity, I include a progress bar to avoid the blank screen while the system is checking the connection. I wrote the code as shown below but it's still not the things I wanted. I have tried so many ways but I didn't make it, how to do it right ? what's wrong with my code, please ?
public class MainActivity extends Activity {
private Handler handler;
private ProgressDialog progress;
private Context context;
ProgressBar progressBar;
public class BackgroundAsyncTask extends
AsyncTask<Void, Integer, Void> {
int myProgress;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"onPostExecute", Toast.LENGTH_LONG).show();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"onPreExecute", Toast.LENGTH_LONG).show();
myProgress = 0;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while (myProgress < 100) {
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(100);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
progressBar.setProgress(values[0]);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (IsReachable(this)) {
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setProgress(0);
new BackgroundAsyncTask().execute();
Toast.makeText(this, "Server on ", Toast.LENGTH_SHORT).show();
Intent i = new Intent(MainActivity.this, Seconde.class);
startActivity(i);
} else {
Toast.makeText(this, "server off ", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("android.net.vpn.SETTINGS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
public static boolean IsReachable(Context context) {
// First, check we have any sort of connectivity
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
boolean isReachable = false;
if (netInfo != null && netInfo.isConnected()) {
// Some sort of connection is open, check if server is reachable
try {
URL url = new URL("http://google.com");//test
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(2 * 1000);
urlc.connect();
isReachable = (urlc.getResponseCode() == 200);
} catch (IOException e) {
//Log.e(TAG, e.getMessage());
}
}
return isReachable;
}
}
Upvotes: 0
Views: 809
Reputation: 191
In your layout, add the progress bar
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:visibility="gone" />
And in your code, when you are checking the server, just set the progress bar visible
mProgressBar.setVisibility(View.VISIBLE);
Then when you done with checking the server, set progress bar to gone
mProgressBar.setVisibility(View.GONE);
Note: The style of progress bar depend on your app theme style
Upvotes: 0
Reputation: 1963
It's not really possible to calculate a percentage of time while connecting to a server - because who knows how long the server will respond. I would recommend showing an indeterminate progress bar (which just animates in a loop without showing any kind of percentage of progress). The only other time you could effectively use a progress bar would be downloading a file - since you know what the total length is, and can make predictions based off the current download speed (or just show a percent).
Upvotes: 2