Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is the way i did it in my application and it works grate.</p> <ol> <li><p>Create AsyncTask thread which will get the location in background.</p> <p>public class GPSmanager extends AsyncTask implements LocationListener {</p> <pre><code>private Context mContext; private final long MIN_TIME_BW_UPDATES = 100000; private final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; public GPSmanager(Context mContext) { super(); this.mContext = mContext; } public String getCurrentCity() { String adress = null; try { Location location = getLocation(); Geocoder gcd = new Geocoder(mContext, Locale.getDefault()); List&lt;Address&gt; addresses = gcd.getFromLocation( location.getLatitude(), location.getLongitude(), 1); if (addresses.size() &gt; 0) { for (int i = 0; i &lt; addresses.size() &amp;&amp; adress == null; i++) adress = addresses.get(i).getLocality(); for (int i = 0; i &lt; addresses.size() &amp;&amp; adress == null; i++) adress = addresses.get(i).getCountryName(); Intent intent = new Intent(MainActivity.BRODCAST_ACTION); intent.putExtra("city", adress); mContext.sendBroadcast(intent); return adress; } } catch (Exception e) { e.printStackTrace(); } return adress; } private Location getLocation() { Location location = null; try { LocationManager locationManager = (LocationManager) mContext .getSystemService(Context.LOCATION_SERVICE); // getting GPS status boolean isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status boolean isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled &amp;&amp; !isNetworkEnabled) { // no network provider is enabled } else { try { Looper.prepare(); } catch (Exception e) { } if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); } } } } } catch (Exception e) { e.printStackTrace(); } return location; } @Override public void onLocationChanged(Location arg0) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } @Override protected Void doInBackground(Void... params) { getCurrentCity(); return null; } </code></pre></li> <li><p>Create Service and run this Thread inside Service</p> <pre><code>public class UpdatesService extends Service { private GPSmanager gpsManager; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { gpsManager = new GPSmanager(this); Utiles.taskLauncher(gpsManager); return super.onStartCommand(intent, flags, startId); } </code></pre> <p>}</p></li> <li><p>Register BrodcastReciver in Activity where you need the location.</p> <p>private BroadcastReceiver receiver = new BroadcastReceiver() {</p> <pre><code> @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(BRODCAST_ACTION)) { String city = intent.getExtras().getString("city"); if (city != null) if (!city.isEmpty()) etSearchCity.setText(intent.getExtras().getString( "city")); } } }; </code></pre></li> <li><p>Register it inside onCreate.</p> <pre><code> registerReceiver(receiver, new IntentFilter(BRODCAST_ACTION)); </code></pre></li> <li><p>There is the best way, I found to do it. The only way to do it quickly and in background - use Service. P.S Don't forget to add service in your Manifest.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload