1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
public class ServiceGMS implements ConnectionCallbacks, OnConnectionFailedListener
{
private Context context;
private Activity activity;
private GoogleApiClient gac;
private Location location;
public static final int ERROR_GMS_API = 100;
public static final int RESOLE_GMS_API = 200;
private static final String LOG_NAME = ServiceGMS.class.getName();
public ServiceGMS(Context context, Activity activity)
{
this.context = context;
this.activity = activity;
gac = new GoogleApiClient.Builder(context, this, this)
.addApi(LocationServices.API)
.build();
location = null;
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
{
Log.i(LOG_NAME,"Not Permission");
Toast.makeText(context, "Not Permission", Toast.LENGTH_LONG).show();
}
else
{
location = LocationServices.FusedLocationApi.getLastLocation(gac);
if(location != null)
{
Log.i(LOG_NAME,"Send CoordinateLocation of Activity");
double lat = parseLtnLat(location.getLatitude());
double lng = parseLtnLat(location.getLongitude());
}
}
}
@Override
public void onConnectionSuspended(int i)
{
Toast.makeText(context, "The Service of Geolocation is disable ", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
int error = connectionResult.getErrorCode();
switch (error)
{
case ConnectionResult.API_UNAVAILABLE :
{
Log.i(LOG_NAME,"Error code GMS it is : "+error);
resolveError(connectionResult,error);
break;
}
case ConnectionResult.SERVICE_DISABLED :
{
Log.i(LOG_NAME,"Error code GMS it is : "+error);
resolveError(connectionResult,error);
break;
}
case ConnectionResult.SERVICE_MISSING :
{
Log.i(LOG_NAME,"Error code GMS it is : "+error);
resolveError(connectionResult,error);
break;
}
case ConnectionResult.DEVELOPER_ERROR :
{
Log.i(LOG_NAME,"Error code GMS it is : "+error);
resolveError(connectionResult,error);
break;
}
case ConnectionResult.NETWORK_ERROR :
{
Log.i(LOG_NAME,"Error code GMS it is : "+error);
resolveError(connectionResult,error);
break;
}
}
}
public void resolveError(ConnectionResult connectionResult,int error)
{
if(!connectionResult.hasResolution())
{
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
googleApiAvailability.getErrorDialog(activity,error, ERROR_GMS_API).show();
}
else
{
try
{
connectionResult.startResolutionForResult(activity,RESOLE_GMS_API);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public void stopGMS()
{
gac.disconnect();
bus.unregister(this);
}
public void startGMS()
{
gac.connect();
bus.register(this);
}
} |
Partager