I'm using Google Play Services for Location in my Android app. This is my first app so I'm not expecting that I have utilized everything perfectly.
My app is to get GPS/Date and send to a server database. I was hoping to learn (and everything works so far) if I've used Google Play Services code properly.
I can only test via Android Studio Project emulator as well.
public class MainActivity extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private String mFormattedDate;
List<MyTask> tasks;
private Button btnNewShowLocation;
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNewShowLocation = (Button) findViewById(R.id.btnNewShowLocation);
tasks = new ArrayList<>();
if (checkPlayServices()) {
buildGoogleApiClient();
}
btnNewShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkPlayServices()) {
requestData("http://10.0.2.2/index.php");
} else {
Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
}
}
});
}
private void displayLocation() {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
mFormattedDate = df.format(c.getTime());
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
} else {
Toast.makeText(getApplicationContext(), "Location not found! \nCheck Location in Settings.", Toast.LENGTH_LONG).show();
}
}
private void requestData(String uri) {
RequestPackage p = new RequestPackage();
p.setMethod("POST");
p.setUri(uri);
p.setParams("longitude", String.valueOf(mLastLocation.getLongitude()));
p.setParams("latitude", String.valueOf(mLastLocation.getLatitude()));
p.setParams("date", String.valueOf(mFormattedDate));
p.setParams("boolean", "true");
MyTask task = new MyTask();
task.execute(p);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
/**
* Method to verify google play services on the device
*/
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(getApplicationContext(),
"This device is not supported.", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
@Override
protected void onResume() {
super.onResume();
checkPlayServices();
}
/**
* Google api callback methods
*/
@Override
public void onConnectionFailed(ConnectionResult result) {
// what's going on here...
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
}
@Override
public void onConnected(Bundle arg0) {
displayLocation();
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}
}