Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- compile 'com.google.android.gms:play-services-location:8.1.0'
- package com.developer.kamalasekar.locationfinder;
- import android.location.Location;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
- private static final String TAG = "MainActivity";
- private TextView mLatitudeTextView;
- private TextView mLongitudeTextView;
- private GoogleApiClient mGoogleApiClient;
- private Location mLocation;
- private LocationRequest mLocationRequest;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mLatitudeTextView = (TextView) findViewById((R.id.latitude_textview));
- mLongitudeTextView = (TextView) findViewById((R.id.longitude_textview));
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- }
- @Override
- public void onConnected(Bundle bundle) {
- mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- if (mLocation != null) {
- mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
- mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));
- } else {
- Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
- }
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
- }
- @Override
- public void onConnectionSuspended(int i) {
- Log.i(TAG, "Connection Suspended");
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
- }
- @Override
- protected void onStart() {
- super.onStart();
- mGoogleApiClient.connect();
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (mGoogleApiClient.isConnected()) {
- mGoogleApiClient.disconnect();
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/latitude"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="Latitude:"
- android:textSize="18sp" />
- <TextView
- android:id="@+id/latitude_textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/latitude"
- android:layout_marginLeft="10dp"
- android:layout_toRightOf="@+id/latitude"
- android:textSize="16sp" />
- <TextView
- android:id="@+id/longitude"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="Longitude:"
- android:layout_marginTop="24dp"
- android:textSize="18sp" />
- <TextView
- android:id="@+id/longitude_textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/longitude"
- android:layout_marginLeft="10dp"
- android:layout_toRightOf="@+id/longitude"
- android:textSize="16sp"/>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.developer.kamalasekar.locationfinder">
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.INTERNET" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <meta-data
- android:name="com.google.android.gms.version"
- android:value="@integer/google_play_services_version" />
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- @Override
- public void onConnected(Bundle bundle) {
- mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- if (mLocation != null) {
- //your stuff
- } else {
- handler.sendEmptyMessageDelayed(1, 2000);
- }
- }
- int count;
- android.os.Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- if (count < 2) {
- mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- if (mLocation == null){
- handler.sendEmptyMessageDelayed(1, 2000);
- }else{
- //your stuff
- }
- count++;
- } else{
- Log.i("MyTag","location not found");
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement