Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. package com.ideabiz.fusedlocationprovider;
  2.  
  3. import android.content.pm.PackageManager;
  4. import android.location.Location;
  5. import android.location.LocationListener;
  6. import android.support.annotation.NonNull;
  7. import android.support.annotation.Nullable;
  8. import android.support.v4.app.ActivityCompat;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.widget.TextView;
  12.  
  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.  
  19. public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
  20. GoogleApiClient.OnConnectionFailedListener, LocationListener {
  21.  
  22.  
  23. TextView txtOutputLat, txtOutputLon;
  24. Location mLastLocation;
  25. private GoogleApiClient mGoogleApiClient;
  26. private LocationRequest mLocationRequest;
  27. String lat, lon;
  28.  
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34.  
  35.  
  36. txtOutputLat = (TextView) findViewById(R.id.textView);
  37. txtOutputLon = (TextView) findViewById(R.id.textView2);
  38.  
  39.  
  40. buildGoogleApiClient();
  41. }
  42.  
  43.  
  44. @Override
  45. public void onConnected(Bundle bundle) {
  46.  
  47.  
  48. mLocationRequest = LocationRequest.create();
  49. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  50. mLocationRequest.setInterval(100); // Update location every second
  51.  
  52. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
  53.  
  54.  
  55. if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  56. // TODO: Consider calling
  57. // ActivityCompat#requestPermissions
  58. // here to request the missing permissions, and then overriding
  59. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  60. // int[] grantResults)
  61. // to handle the case where the user grants the permission. See the documentation
  62. // for ActivityCompat#requestPermissions for more details.
  63. return;
  64. }
  65. mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
  66. mGoogleApiClient);
  67. if (mLastLocation != null) {
  68. lat = String.valueOf(mLastLocation.getLatitude());
  69. lon = String.valueOf(mLastLocation.getLongitude());
  70.  
  71. }
  72. updateUI();
  73. }
  74.  
  75. @Override
  76. public void onConnectionSuspended(int i) {
  77.  
  78. }
  79.  
  80. @Override
  81. public void onLocationChanged(Location location) {
  82. lat = String.valueOf(location.getLatitude());
  83. lon = String.valueOf(location.getLongitude());
  84. updateUI();
  85. }
  86.  
  87. @Override
  88. public void onStatusChanged(String s, int i, Bundle bundle) {
  89.  
  90. }
  91.  
  92. @Override
  93. public void onProviderEnabled(String s) {
  94.  
  95. }
  96.  
  97. @Override
  98. public void onProviderDisabled(String s) {
  99.  
  100. }
  101.  
  102. @Override
  103. public void onConnectionFailed(ConnectionResult connectionResult) {
  104. buildGoogleApiClient();
  105. }
  106.  
  107. synchronized void buildGoogleApiClient() {
  108. mGoogleApiClient = new GoogleApiClient.Builder(this)
  109. .addConnectionCallbacks(this)
  110. .addOnConnectionFailedListener(this)
  111. .addApi(LocationServices.API)
  112. .build();
  113.  
  114.  
  115. }
  116.  
  117. @Override
  118. protected void onStart() {
  119. super.onStart();
  120. mGoogleApiClient.connect();
  121. }
  122.  
  123. @Override
  124. protected void onDestroy() {
  125. super.onDestroy();
  126. mGoogleApiClient.disconnect();
  127. }
  128.  
  129. void updateUI() {
  130. txtOutputLat.setText(lat);
  131. txtOutputLon.setText(lon);
  132. }
  133. }
  134.  
  135. E/AndroidRuntime: FATAL EXCEPTION: main
  136. Process: com.ideabiz.fusedlocationprovider, PID: 18114
  137. java.lang.ClassCastException: com.ideabiz.fusedlocationprovider.MainActivity cannot be cast to com.google.android.gms.location.LocationListener
  138. at com.ideabiz.fusedlocationprovider.MainActivity.onConnected(MainActivity.java:52)
  139.  
  140. <?xml version="1.0" encoding="utf-8"?>
  141. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  142. package="com.ideabiz.fusedlocationprovider">
  143. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  144. <application
  145. android:allowBackup="true"
  146. android:icon="@mipmap/ic_launcher"
  147. android:label="@string/app_name"
  148. android:supportsRtl="true"
  149. android:theme="@style/AppTheme">
  150. <activity android:name=".MainActivity">
  151. <intent-filter>
  152. <action android:name="android.intent.action.MAIN" />
  153.  
  154. <category android:name="android.intent.category.LAUNCHER" />
  155. </intent-filter>
  156. </activity>
  157. <meta-data android:name="com.google.android.gms.version"
  158. android:value="@integer/google_play_services_version" />
  159. </application>
  160.  
  161. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement