标签:
1 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 2 private Button bt_main_checkin; 3 private Toolbar toolbar; 4 private TextView tv_main_test; 5 6 private LocationManager mLocationManager; 7 8 private static final int TEN_SECONDS = 10000; 9 private static final int TEN_METERS = 10; 10 private static final int TWO_MINUTES = 1000 * 60 * 2; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 initView(); 17 bt_main_checkin.setOnClickListener(this); 18 setSupportActionBar(toolbar); 19 mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 20 Location LatLng = setup(); 21 // Toast.makeText(this, LatLng.getLatitude() + "|" + LatLng.getLongitude(), Toast.LENGTH_SHORT).show(); 22 tv_main_test.setText(LatLng.getLatitude() + "|" + LatLng.getLongitude()); 23 } 24 25 private void initView() { 26 toolbar = (Toolbar) findViewById(R.id.toolbar); 27 bt_main_checkin = (Button) findViewById(R.id.bt_main_checkin); 28 tv_main_test= (TextView) findViewById(R.id.tv_main_test); 29 } 30 31 @Override 32 protected void onResume() { 33 super.onResume(); 34 setup(); 35 } 36 37 @Override 38 protected void onStop() { 39 super.onStop(); 40 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 41 return; 42 } 43 mLocationManager.removeUpdates(listener); 44 } 45 46 @Override 47 protected void onStart() { 48 super.onStart(); 49 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 50 final boolean gpsenable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 51 if (!gpsenable) { 52 new EnableGpsDialogFragment().show(getFragmentManager(), "enableGpsDialog"); 53 } 54 } 55 56 @Override 57 public void onClick(View v) { 58 59 } 60 private Location setup() { 61 Location gpsLocation = null; 62 Location networkLocation = null; 63 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 64 // TODO: Consider calling 65 new EnableGpsDialogFragment().show(getFragmentManager(), "enableGpsDialog"); 66 new EnableNetworkDialogFragment().show(getFragmentManager(), "enableNetworkDialog"); 67 return null; 68 } 69 mLocationManager.removeUpdates(listener); 70 gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps); 71 networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER, R.string.not_support_network); 72 if (gpsLocation != null && networkLocation != null) { 73 return getBetterLocation(gpsLocation, networkLocation); 74 } else if (gpsLocation != null) { 75 return gpsLocation; 76 } else if (networkLocation != null) { 77 return networkLocation; 78 } 79 return null; 80 } 81 82 private Location requestUpdatesFromProvider(final String provider, final int errorResId) { 83 Location location = null; 84 if (mLocationManager.isProviderEnabled(provider)) { 85 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 86 // TODO: Consider calling 87 return null; 88 } 89 mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS, listener); 90 location = mLocationManager.getLastKnownLocation(provider); 91 } else { 92 Toast.makeText(this, errorResId, Toast.LENGTH_SHORT).show(); 93 } 94 return location; 95 } 96 97 private final LocationListener listener = new LocationListener() { 98 @Override 99 public void onLocationChanged(Location location) { 100 // TODO: 2016/3/23 updata ui in it 101 102 } 103 104 @Override 105 public void onStatusChanged(String provider, int status, Bundle extras) { 106 107 } 108 109 @Override 110 public void onProviderEnabled(String provider) { 111 112 } 113 114 @Override 115 public void onProviderDisabled(String provider) { 116 117 } 118 }; 119 120 protected Location getBetterLocation(Location newLocation, Location currentBestLocation) { 121 if (currentBestLocation == null) { 122 // A new location is always better than no location 123 return newLocation; 124 } 125 // If it‘s been more than two minutes since the current location, use the new location 126 // because the user has likely moved. 127 long timeDelta = newLocation.getTime() - currentBestLocation.getTime(); 128 boolean isSignficantlyNewer = timeDelta > TWO_MINUTES; 129 boolean isSignficantlyOlder = timeDelta < -TWO_MINUTES; 130 boolean isNewer = timeDelta > 0; 131 if (isSignficantlyNewer) { 132 return newLocation; 133 } else if (isSignficantlyOlder) { 134 return currentBestLocation; 135 } 136 // Determine location quality using a combination of timeliness and accuracy 137 int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy()); 138 boolean isLessAccurate = accuracyDelta > 0; 139 boolean isMoreAccurate = accuracyDelta < 0; 140 boolean isSignificantlyLessAccurate = accuracyDelta > 200; 141 142 boolean isFromSameProvider = isSameProvider(newLocation.getProvider(), 143 currentBestLocation.getProvider()); 144 if (isMoreAccurate) { 145 return newLocation; 146 } else if (isNewer && !isLessAccurate) { 147 return newLocation; 148 } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 149 return newLocation; 150 } 151 return currentBestLocation; 152 } 153 154 /** 155 * Checks whether two providers are the same 156 */ 157 private boolean isSameProvider(String provider1, String provider2) { 158 if (provider1 == null) { 159 return provider2 == null; 160 } 161 return provider1.equals(provider2); 162 } 163 164 165 @Override 166 public boolean onCreateOptionsMenu(Menu menu) { 167 // Inflate the menu; this adds items to the action bar if it is present. 168 getMenuInflater().inflate(R.menu.menu_main, menu); 169 return true; 170 } 171 172 @Override 173 public boolean onOptionsItemSelected(MenuItem item) { 174 // Handle action bar item clicks here. The action bar will 175 // automatically handle clicks on the Home/Up button, so long 176 // as you specify a parent activity in AndroidManifest.xml. 177 int id = item.getItemId(); 178 switch (id){ 179 case R.id.action_settings: 180 break; 181 case R.id.action_register: 182 break; 183 default: 184 break; 185 } 186 //noinspection SimplifiableIfStatement 187 // if (id == R.id.action_settings) { 188 // return true; 189 // } 190 191 return true; 192 } 193 194 }
详细见官网
标签:
原文地址:http://www.cnblogs.com/meimeng/p/5316594.html