Skip to content

Instantly share code, notes, and snippets.

@himanshu-soni
Created October 31, 2014 19:22
Show Gist options
  • Save himanshu-soni/1a74b095b699f5228b56 to your computer and use it in GitHub Desktop.
Save himanshu-soni/1a74b095b699f5228b56 to your computer and use it in GitHub Desktop.
Base Activity to detect internet connection GPS disabled. It shows dialog to enable them too :)
package com.djhs16.location;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
/**
* Base activity to check GPS disabled and Internet <br/>
* this activity requires following permission(s) to be added in the AndroidManifest.xml file:
*
* <ul>
* <li>android.permission.ACCESS_FINE_LOCATION</li>
* <li>android.permission.INTERNET</li>
* <li>android.permission.ACCESS_NETWORK_STATE</li>
*
* </ul>
*/
public class BaseActivity extends Activity implements LocationListener {
protected LocationManager mLocationManager;
private static final int GPS_ENABLE_REQUEST = 0x1001;
private static final int WIFI_ENABLE_REQUEST = 0x1006;
private BroadcastReceiver mNetworkDetectReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
checkInternetConnection();
}
};
private AlertDialog mInternetDialog;
private AlertDialog mGPSDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
registerReceiver(mNetworkDetectReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
protected void onDestroy() {
mLocationManager.removeUpdates(this);
unregisterReceiver(mNetworkDetectReceiver);
super.onDestroy();
}
private void checkInternetConnection() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
} else {
showNoInternetDialog();
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
if (provider.equals(LocationManager.GPS_PROVIDER)) {
showGPSDiabledDialog();
}
}
private void showNoInternetDialog() {
if (mInternetDialog != null && mInternetDialog.isShowing()) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Internet Disabled!");
builder.setMessage("No active Internet connection found.");
builder.setPositiveButton("Turn On", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivityForResult(gpsOptionsIntent, WIFI_ENABLE_REQUEST);
}
}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mInternetDialog = builder.create();
mInternetDialog.show();
}
public void showGPSDiabledDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("GPS Disabled");
builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device");
builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST);
}
}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mGPSDialog = builder.create();
mGPSDialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GPS_ENABLE_REQUEST) {
if (mLocationManager == null) {
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
showGPSDiabledDialog();
}
} else if (requestCode == WIFI_ENABLE_REQUEST) {
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment