Guest User

Untitled

a guest
Jul 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. package com.location;
  2.  
  3. import java.io.IOException;
  4. import java.util.*;
  5. import android.widget.*;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.app.Activity;
  9. import android.app.ProgressDialog;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.location.*;
  13. import android.content.*;
  14.  
  15. public class LocationActivity extends Activity {
  16.  
  17. Button addressButton;
  18. TextView locationText;
  19. TextView addressText;
  20. Location currentLocation;
  21. double currentLatitude;
  22. double currentLongitude;
  23. ProgressDialog progressDialog = null;
  24. Handler mHandler = new Handler()
  25. {
  26. public void handleMessage(android.os.Message msg)
  27. {
  28. super.handleMessage(msg);
  29.  
  30. switch (msg.what)
  31. {
  32. case 10:
  33. progressDialog.dismiss();
  34. break;
  35. }
  36. }
  37.  
  38. };
  39. @Override
  40. public void onCreate(Bundle savedInstanceState) {
  41.  
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.main2);
  44.  
  45. addressText = (TextView)findViewById(R.id.addressText);
  46. locationText = (TextView)findViewById(R.id.locationText);
  47. // addressButton = (Button)findViewById(R.id.addressButton);
  48.  
  49. this.addressText.setText("ready");
  50.  
  51. LocationManager locationManager =
  52. (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
  53.  
  54. LocationListener locationListener = new LocationListener() {
  55. public void onLocationChanged(Location location) {
  56. currentLocation = location;
  57. currentLatitude = currentLocation.getLatitude();
  58. currentLongitude = currentLocation.getLongitude();
  59. locationText.setText(currentLatitude + ", " + currentLongitude);
  60. }
  61. public void onStatusChanged(
  62. String provider, int status, Bundle extras) {}
  63. public void onProviderEnabled(String provider) {}
  64. public void onProviderDisabled(String provider) {}
  65. };
  66.  
  67. locationManager.requestLocationUpdates(
  68. LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  69.  
  70. // this.addressButton.setOnClickListener(new OnClickListener() {
  71. // public void onClick(View v){
  72.  
  73. progressDialog = ProgressDialog.show(LocationActivity.this,"Please Wait", "Loading Date", true);
  74.  
  75. new Thread()
  76. {
  77. public void Run()
  78. {
  79. try
  80. {
  81. getAddress();
  82. progressDialog.dismiss();
  83.  
  84. }
  85. catch(Exception e)
  86. {
  87.  
  88. }
  89.  
  90. }
  91. }.start();
  92.  
  93. }
  94.  
  95.  
  96.  
  97. void getAddress(){
  98.  
  99. StringBuilder result = new StringBuilder();
  100. try{
  101. Geocoder gcd = new Geocoder(this, Locale.getDefault());
  102. List<Address> addresses =
  103. gcd.getFromLocation(currentLatitude, currentLongitude,100);
  104. if (addresses.size() > 0)
  105. {
  106. Address address = addresses.get(0);
  107. int maxIndex = address.getMaxAddressLineIndex();
  108. for (int x = 0; x <= maxIndex; x++ ){
  109. result.append(address.getAddressLine(x));
  110. result.append(",");
  111. }
  112. }
  113. //pd.dismiss();
  114. mHandler.sendEmptyMessage(10);
  115. addressText.setText(result.toString());
  116.  
  117. }
  118. catch(IOException ex){
  119. addressText.setText(ex.getMessage().toString());
  120. }
  121. }
  122.  
  123. void updateLocation(Location location){
  124.  
  125. }
  126. }
Add Comment
Please, Sign In to add comment