Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
  2. GoogleApiClient.OnConnectionFailedListener,LocationListener{
  3.  
  4. private GoogleMap mMap;
  5. private GoogleApiClient mGoogleApiClient;
  6. private LocationRequest mLocationRequest;
  7. private Marker mCurrLocationMarker;
  8. private TextView mLatitude, mLongitude;
  9. private Button mNextBtn;
  10. private Location location;
  11.  
  12. private int mInterval = 5000;
  13. private static final String TAG = "BroadcastTest";
  14. private Intent intent;
  15. Handler mHandler;
  16.  
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_maps);
  22.  
  23. mLatitude = (TextView) findViewById(R.id.tv_lat);
  24. mLongitude = (TextView) findViewById(R.id.tv_long);
  25. mNextBtn = (Button) findViewById(R.id.btn_next);
  26. intent = new Intent(this, BroadcastService.class);
  27.  
  28. mNextBtn.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View view) {
  31. Intent intent = new Intent(MapsActivity.this, LatLongListActivity.class);
  32. startActivity(intent);
  33. }
  34. });
  35. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  36. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  37. .findFragmentById(R.id.map);
  38. mapFragment.getMapAsync(this);
  39. startRepeatingTask();
  40. mHandler = new Handler();
  41.  
  42. /*Thread t = new Thread() {
  43.  
  44. @Override
  45. public void run() {
  46. try {
  47. while (!isInterrupted()) {
  48. Thread.sleep(10000);
  49. runOnUiThread(new Runnable() {
  50. @Override
  51. public void run() {
  52. // update TextView here!
  53. Toast.makeText(getApplicationContext(),"Update",Toast.LENGTH_SHORT).show();
  54. }
  55. });
  56. }
  57. } catch (InterruptedException e) {
  58. }
  59. }
  60. };
  61.  
  62. t.start();*/
  63.  
  64. }
  65.  
  66. @Override
  67. public void onMapReady(GoogleMap googleMap) {
  68. mMap = googleMap;
  69.  
  70. // Add a marker in Sydney and move the camera
  71. mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  72.  
  73. //Initialize Google Play Services
  74. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  75. if (ContextCompat.checkSelfPermission(this,
  76. Manifest.permission.ACCESS_FINE_LOCATION)
  77. == PackageManager.PERMISSION_GRANTED) {
  78. buildGoogleApiClient();
  79. mMap.setMyLocationEnabled(true);
  80. }
  81. } else {
  82. buildGoogleApiClient();
  83. mMap.setMyLocationEnabled(true);
  84. }
  85. }
  86.  
  87. Runnable mStatusChecker = new Runnable() {
  88. @Override
  89. public void run() {
  90. try {
  91.  
  92. onLocationChanged(location);
  93.  
  94. Toast.makeText(getApplicationContext(),"Update",Toast.LENGTH_SHORT).show();
  95. } finally {
  96. mHandler.postDelayed(mStatusChecker, mInterval); // set mInterval = 300000
  97. }
  98. }
  99. };
  100.  
  101. void startRepeatingTask() {
  102. mStatusChecker.run();
  103. }
  104.  
  105. void stopRepeatingTask() {
  106. mHandler.removeCallbacks(mStatusChecker);
  107. }
  108.  
  109. public void onLocationChanged(Location location) {
  110. // called when the listener is notified with a location update from the GPS
  111.  
  112.  
  113. double currentLatitude = location.getLatitude();
  114. double currentLogitude = location.getLongitude();
  115. LatLng latLng = new LatLng(currentLatitude, currentLogitude);
  116. MarkerOptions markerOptions = new MarkerOptions();
  117. markerOptions.position(latLng);
  118. markerOptions.title("Current Position");
  119. markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
  120. mCurrLocationMarker = mMap.addMarker(markerOptions);
  121.  
  122. //move map camera
  123. mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  124. mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
  125.  
  126. //move map camera
  127. mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  128. mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
  129.  
  130. //stop location updates
  131. if (mGoogleApiClient != null) {
  132. LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this);
  133.  
  134. String lat = String.valueOf(location.getLatitude());
  135. String longi = String.valueOf(location.getLongitude());
  136.  
  137. mLatitude.setText("Latitude : " + lat);
  138. mLongitude.setText("Logitude : " + longi);
  139.  
  140.  
  141. SharedPreferences sp = getSharedPreferences("key", 0);
  142. SharedPreferences.Editor sedt = sp.edit();
  143. sedt.putString("textvalue", mLatitude.getText().toString());
  144. sedt.putString("txtopertaive", mLongitude.getText().toString());
  145. sedt.commit();
  146. }
  147. }
  148.  
  149. public void onProviderDisabled(String provider) {
  150. // called when the GPS provider is turned off (user turning off the GPS on the phone)
  151. }
  152.  
  153. public void onProviderEnabled(String provider) {
  154. // called when the GPS provider is turned on (user turning on the GPS on the phone)
  155. }
  156.  
  157. public void onStatusChanged(String provider, int status, Bundle extras) {
  158. // called when the status of the GPS provider changes
  159. }
  160.  
  161.  
  162. private void buildGoogleApiClient() {
  163.  
  164. mGoogleApiClient = new GoogleApiClient.Builder(this)
  165. .addConnectionCallbacks(this)
  166. .addOnConnectionFailedListener(this)
  167. .addApi(LocationServices.API)
  168. .build();
  169. mGoogleApiClient.connect();
  170. }
  171.  
  172. @Override
  173. public void onConnected(@Nullable Bundle bundle) {
  174.  
  175. mLocationRequest = new LocationRequest();
  176. mLocationRequest.setInterval(1000);
  177. mLocationRequest.setFastestInterval(1000);
  178. mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  179. if (ContextCompat.checkSelfPermission(this,
  180. Manifest.permission.ACCESS_FINE_LOCATION)
  181. == PackageManager.PERMISSION_GRANTED) {
  182. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest,this);
  183. }
  184.  
  185. }
  186.  
  187. @Override
  188. public void onConnectionSuspended(int i) {
  189.  
  190. }
  191.  
  192. @Override
  193. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  194.  
  195. }
  196.  
  197. public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
  198. public boolean checkLocationPermission(){
  199. if (ContextCompat.checkSelfPermission(this,
  200. Manifest.permission.ACCESS_FINE_LOCATION)
  201. != PackageManager.PERMISSION_GRANTED) {
  202.  
  203. // Asking user if explanation is needed
  204. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  205. Manifest.permission.ACCESS_FINE_LOCATION)) {
  206.  
  207. // Show an expanation to the user *asynchronously* -- don't block
  208. // this thread waiting for the user's response! After the user
  209. // sees the explanation, try again to request the permission.
  210.  
  211. //Prompt the user once explanation has been shown
  212. ActivityCompat.requestPermissions(this,
  213. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  214. MY_PERMISSIONS_REQUEST_LOCATION);
  215.  
  216.  
  217. } else {
  218. // No explanation needed, we can request the permission.
  219. ActivityCompat.requestPermissions(this,
  220. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  221. MY_PERMISSIONS_REQUEST_LOCATION);
  222. }
  223. return false;
  224. } else {
  225. return true;
  226. }
  227. }
  228.  
  229. Runnable mStatusChecker = new Runnable() {
  230. @Override
  231. public void run() {
  232. try {
  233. <call you location fetching method here || simply call location.get... methods here>
  234.  
  235. } finally {
  236. mHandler.postDelayed(mStatusChecker, mInterval); // set mInterval = 300000
  237. }
  238. }
  239. };
  240.  
  241. void startRepeatingTask() {
  242. mStatusChecker.run();
  243. }
  244.  
  245. void stopRepeatingTask() {
  246. mHandler.removeCallbacks(mStatusChecker);
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement