Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. compile 'com.google.android.gms:play-services-location:8.1.0'
  2.  
  3. package com.developer.kamalasekar.locationfinder;
  4.  
  5. import android.location.Location;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.util.Log;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import com.google.android.gms.common.ConnectionResult;
  14. import com.google.android.gms.common.api.GoogleApiClient;
  15. import com.google.android.gms.location.LocationRequest;
  16. import com.google.android.gms.location.LocationServices;
  17.  
  18. public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  19.  
  20. private static final String TAG = "MainActivity";
  21. private TextView mLatitudeTextView;
  22. private TextView mLongitudeTextView;
  23. private GoogleApiClient mGoogleApiClient;
  24. private Location mLocation;
  25. private LocationRequest mLocationRequest;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. mLatitudeTextView = (TextView) findViewById((R.id.latitude_textview));
  32. mLongitudeTextView = (TextView) findViewById((R.id.longitude_textview));
  33.  
  34. mGoogleApiClient = new GoogleApiClient.Builder(this)
  35. .addConnectionCallbacks(this)
  36. .addOnConnectionFailedListener(this)
  37. .addApi(LocationServices.API)
  38. .build();
  39. }
  40.  
  41. @Override
  42. public void onConnected(Bundle bundle) {
  43. mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  44.  
  45. if (mLocation != null) {
  46. mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
  47. mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));
  48. } else {
  49. Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
  50. }
  51. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
  52.  
  53.  
  54.  
  55.  
  56. }
  57.  
  58. @Override
  59. public void onConnectionSuspended(int i) {
  60. Log.i(TAG, "Connection Suspended");
  61. mGoogleApiClient.connect();
  62. }
  63.  
  64. @Override
  65. public void onConnectionFailed(ConnectionResult connectionResult) {
  66. Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
  67. }
  68.  
  69. @Override
  70. protected void onStart() {
  71. super.onStart();
  72. mGoogleApiClient.connect();
  73. }
  74.  
  75. @Override
  76. protected void onStop() {
  77. super.onStop();
  78. if (mGoogleApiClient.isConnected()) {
  79. mGoogleApiClient.disconnect();
  80. }
  81. }
  82. }
  83.  
  84. <?xml version="1.0" encoding="utf-8"?>
  85. <RelativeLayout
  86. xmlns:android="http://schemas.android.com/apk/res/android"
  87. xmlns:tools="http://schemas.android.com/tools"
  88. android:layout_width="match_parent"
  89. android:layout_height="match_parent"
  90. android:paddingLeft="@dimen/activity_horizontal_margin"
  91. android:paddingRight="@dimen/activity_horizontal_margin"
  92. android:paddingTop="@dimen/activity_vertical_margin"
  93. android:paddingBottom="@dimen/activity_vertical_margin"
  94. tools:context=".MainActivity">
  95.  
  96. <TextView
  97. android:id="@+id/latitude"
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:layout_alignParentLeft="true"
  101. android:layout_alignParentTop="true"
  102. android:text="Latitude:"
  103. android:textSize="18sp" />
  104. <TextView
  105. android:id="@+id/latitude_textview"
  106. android:layout_width="wrap_content"
  107. android:layout_height="wrap_content"
  108. android:layout_alignBaseline="@+id/latitude"
  109. android:layout_marginLeft="10dp"
  110. android:layout_toRightOf="@+id/latitude"
  111. android:textSize="16sp" />
  112. <TextView
  113. android:id="@+id/longitude"
  114. android:layout_width="wrap_content"
  115. android:layout_height="wrap_content"
  116. android:layout_alignParentLeft="true"
  117. android:layout_alignParentTop="true"
  118. android:text="Longitude:"
  119. android:layout_marginTop="24dp"
  120. android:textSize="18sp" />
  121. <TextView
  122. android:id="@+id/longitude_textview"
  123. android:layout_width="wrap_content"
  124. android:layout_height="wrap_content"
  125. android:layout_alignBaseline="@+id/longitude"
  126. android:layout_marginLeft="10dp"
  127. android:layout_toRightOf="@+id/longitude"
  128. android:textSize="16sp"/>
  129.  
  130. </RelativeLayout>
  131.  
  132. <?xml version="1.0" encoding="utf-8"?>
  133. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  134. package="com.developer.kamalasekar.locationfinder">
  135.  
  136.  
  137. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  138. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  139.  
  140. <uses-permission android:name="android.permission.INTERNET" />
  141.  
  142. <application
  143. android:allowBackup="true"
  144. android:icon="@mipmap/ic_launcher"
  145. android:label="@string/app_name"
  146. android:supportsRtl="true"
  147. android:theme="@style/AppTheme">
  148.  
  149. <meta-data
  150. android:name="com.google.android.gms.version"
  151. android:value="@integer/google_play_services_version" />
  152.  
  153. <activity android:name=".MainActivity">
  154. <intent-filter>
  155. <action android:name="android.intent.action.MAIN" />
  156.  
  157. <category android:name="android.intent.category.LAUNCHER" />
  158. </intent-filter>
  159. </activity>
  160.  
  161. </application>
  162.  
  163. </manifest>
  164.  
  165. @Override
  166. public void onConnected(Bundle bundle) {
  167. mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  168. if (mLocation != null) {
  169. //your stuff
  170. } else {
  171. handler.sendEmptyMessageDelayed(1, 2000);
  172. }
  173. }
  174.  
  175. int count;
  176. android.os.Handler handler = new Handler() {
  177. @Override
  178. public void handleMessage(Message msg) {
  179. if (count < 2) {
  180. mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  181. if (mLocation == null){
  182. handler.sendEmptyMessageDelayed(1, 2000);
  183. }else{
  184. //your stuff
  185. }
  186. count++;
  187. } else{
  188. Log.i("MyTag","location not found");
  189. }
  190. }
  191. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement