Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.GooglePlayServicesUtil;
- 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.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import android.app.Activity;
- import android.location.Location;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.AsyncTask;
- import android.text.method.ScrollingMovementMethod;
- import android.widget.ProgressBar;
- public class MainActivity extends Activity implements ConnectionCallbacks,
- OnConnectionFailedListener {
- // NEW CONTENT
- private static final String TAG = MainActivity.class.getSimpleName();
- private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
- private GoogleApiClient mGoogleApiClient;
- private Location mLastLocation;
- //ORIGINAL CONTENT
- TextView output;
- ProgressBar pb;
- List<MyTask> tasks;
- private Button btnNewShowLocation;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- output = (TextView) findViewById(R.id.textView);
- output.setMovementMethod(new ScrollingMovementMethod());
- pb = (ProgressBar) findViewById(R.id.progressBar1);
- pb.setVisibility(View.INVISIBLE);
- btnNewShowLocation = (Button) findViewById(R.id.btnNewShowLocation);
- tasks = new ArrayList<>();
- // NEW CONTENT
- if (checkPlayServices()) {
- // Building the GoogleApi client
- buildGoogleApiClient();
- }
- // ORIGINAL CONTENT
- btnNewShowLocation.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (isOnline()) {
- requestData("http://10.0.2.2/index2.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");
- String formattedDate = df.format(c.getTime());
- if (mLastLocation != null) {
- double latitude = mLastLocation.getLatitude();
- double longitude = mLastLocation.getLongitude();
- Toast.makeText(getApplicationContext(), "Latitude = " + latitude + "nLongitude = " + longitude + "nDate = " + formattedDate, Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(getApplicationContext(), "Location not found! nIs Location turned ON in Settings?", Toast.LENGTH_LONG).show();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.action_get_data) {
- }
- return false;
- }
- private void requestData(String uri) {
- RequestPackage p = new RequestPackage();
- p.setMethod("POST");
- p.setUri(uri);
- p.setParams("longitude", "Fake Value 1");
- p.setParams("latitude", "Fake Value 2");
- p.setParams("date", "Fake Value 3");
- MyTask task = new MyTask();
- task.execute(p);
- }
- protected void updateDisplay(String result) {
- output.append(result + "n");
- }
- protected boolean isOnline() {
- ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfo = cm.getActiveNetworkInfo();
- if (netInfo != null && netInfo.isConnectedOrConnecting()) {
- return true;
- } else {
- return false;
- }
- }
- // NEW CONTENT
- 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) {
- }
- @Override
- public void onConnectionSuspended(int arg0) {
- mGoogleApiClient.connect();
- }
- // ORIGINAL CONTENT
- private class MyTask extends AsyncTask<RequestPackage, String, String> {
- @Override
- protected void onPreExecute() {
- if (tasks.size() == 0) {
- pb.setVisibility(View.VISIBLE);
- }
- tasks.add(this);
- }
- @Override
- protected String doInBackground(RequestPackage... params) {
- String content = HttpManager.getData(params[0]);
- return content;
- }
- @Override
- protected void onPostExecute(String result) {
- updateDisplay(result);
- tasks.remove(this);
- if (tasks.size() == 0) {
- pb.setVisibility(View.INVISIBLE);
- }
- }
- @Override
- protected void onProgressUpdate(String... values) {
- //updateDisplay(values[0]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement