Note that there are some explanatory texts on larger screens.

plurals
  1. POGPS LocationManager and ListenerManager Management in a Base Activity
    primarykey
    data
    text
    <p>My need is that, all most of my activities need location of user to get updated data which is based on location. So, instead of define in all activities seperately I define a base activity (BaseActivity.java) and declared all activities have to inherite this class. </p> <p>For example:</p> <pre><code>package com.example; import com.example.tools.Utilities; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnMenuItemClickListener; import android.view.Window; public class BaseActivity extends Activity { protected static final String GpsTag = "GPS"; private static LocationManager mLocManager; private static LocationListener mLocListener; private static double mDefaultLatitude = 41.0793; private static double mDefaultLongtitude = 29.0461; private Location CurrentBestLocation; private float mMinDistanceForGPSProvider = 200; private float mMinDistanceForNetworkProvider = 1000; private float mMinDistanceForPassiveProvider = 3000; private long mMinTime = 0; private UserLocation mCurrentLocation = new UserLocation(mDefaultLatitude, mDefaultLongtitude); private Boolean showLocationNotification = false; protected Activity mActivity; private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { UserManagement.logOut(getApplication()); Utilities.setApplicationTitle(mActivity); // finish(); } }; private BroadcastReceiver mLoggedInReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Utilities.setApplicationTitle(mActivity); // finish(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); super.registerReceiver(mLoggedOutReceiver, new IntentFilter( Utilities.LOG_OUT_ACTION)); super.registerReceiver(mLoggedInReceiver, new IntentFilter( Utilities.LOG_IN_ACTION)); mActivity = this; if (mLocManager == null || mLocListener == null) { mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mLocListener = new LocationListener() { @Override public void onLocationChanged(Location location) { Log.i(GpsTag, "Location Changed"); if (isBetterLocation(location, CurrentBestLocation)) { sendBroadcast(new Intent( Utilities.LOCATION_CHANGED_ACTION)); mCurrentLocation.Latitude = location.getLatitude(); mCurrentLocation.Longtitude = location.getLongitude(); if (location.hasAccuracy()) { mCurrentLocation.Accuracy = location.getAccuracy(); } UserManagement.UserLocation = mCurrentLocation; mCurrentLocation.Latitude = location.getLatitude(); mCurrentLocation.Longtitude = location.getLongitude(); if (location.hasAccuracy()) { mCurrentLocation.Accuracy = location.getAccuracy(); } UserManagement.UserLocation = mCurrentLocation; CurrentBestLocation = location; } } @Override public void onProviderDisabled(String provider) { chooseProviderAndSetLocation(); } @Override public void onProviderEnabled(String provider) { chooseProviderAndSetLocation(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }; chooseProviderAndSetLocation(); } } private void chooseProviderAndSetLocation() { Location loc = null; if (mLocManager == null) return; mLocManager.removeUpdates(mLocListener); if (mLocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { loc = mLocManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } else if (mLocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { loc = mLocManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); } for (String providerName : mLocManager.getProviders(true)) { float minDistance = mMinDistanceForPassiveProvider; if (providerName.equals("network")) minDistance = mMinDistanceForNetworkProvider; else if (providerName.equals("gps")) minDistance = mMinDistanceForGPSProvider; mLocManager.requestLocationUpdates(providerName, mMinTime, minDistance, mLocListener); Log.i(GpsTag, providerName + " listener binded..."); } if (loc != null) { if (mCurrentLocation == null) mCurrentLocation = new UserLocation(mDefaultLatitude, mDefaultLongtitude); mCurrentLocation.Latitude = loc.getLatitude(); mCurrentLocation.Longtitude = loc.getLongitude(); mCurrentLocation.Accuracy = loc.hasAccuracy() ? loc.getAccuracy() : 0; UserManagement.UserLocation = mCurrentLocation; CurrentBestLocation = loc; } else { if (showLocationNotification) { Utilities .warnUserWithToast( getApplication(), "Default Location"); } UserManagement.UserLocation = new UserLocation(mDefaultLatitude, mDefaultLongtitude); } } private static final int TWO_MINUTES = 1000 * 60 * 2; protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta &gt; TWO_MINUTES; boolean isSignificantlyOlder = timeDelta &lt; -TWO_MINUTES; boolean isNewer = timeDelta &gt; 0; // If it's been more than two minutes since the current location, use // the new location // because the user has likely moved if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be // worse } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation .getAccuracy()); boolean isLessAccurate = accuracyDelta &gt; 0; boolean isMoreAccurate = accuracyDelta &lt; 0; boolean isSignificantlyLessAccurate = accuracyDelta &gt; 200; // Check if the old and new location are from the same providerl boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and // accuracy if (isMoreAccurate) { return true; } else if (isNewer &amp;&amp; !isLessAccurate) { return true; } else if (isNewer &amp;&amp; !isSignificantlyLessAccurate &amp;&amp; isFromSameProvider) { return true; } return false; } private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } @Override public boolean moveTaskToBack(boolean nonRoot) { Utilities.warnUserWithToast(getApplicationContext(), "moveTaskToBack: " + nonRoot); return super.moveTaskToBack(nonRoot); } @Override protected void onDestroy() { super.onDestroy(); mLocManager.removeUpdates(mLocListener); super.unregisterReceiver(mLoggedOutReceiver); super.unregisterReceiver(mLoggedInReceiver); } @Override protected void onRestart() { super.onRestart(); Utilities.warnUserWithToast(getApplicationContext(), "onRestart: " + getPackageName()); } @Override protected void onPause() { super.onPause(); mLocManager.removeUpdates(mLocListener); Utilities.warnUserWithToast(getApplicationContext(), "onPause: " + getPackageName()); } @Override protected void onResume() { super.onResume(); Utilities.warnUserWithToast(getApplicationContext(), "onResume: " ); chooseProviderAndSetLocation(); } @Override protected void finalize() throws Throwable { Utilities.warnUserWithToast(getApplicationContext(), "finalize: " ); super.finalize(); } @Override protected void onPostResume() { Utilities.warnUserWithToast(getApplicationContext(), "onPostResume: "); super.onPostResume(); } @Override protected void onStart() { Utilities.warnUserWithToast(getApplicationContext(), "onStart: " ); super.onStart(); } @Override protected void onStop() { Utilities.warnUserWithToast(getApplicationContext(), "onStop: "); super.onStop(); } public void setLocationNotification(Boolean show) { this.showLocationNotification = show; } @Override public boolean onCreateOptionsMenu(final Menu menu) { if (menu != null &amp;&amp; UserManagement.CurrentUser != null) { final MenuItem miExit = menu.add("Log Out"); miExit.setIcon(R.drawable.menu_exit); miExit.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { sendBroadcast(new Intent(Utilities.LOG_OUT_ACTION)); menu.removeItem(miExit.getItemId()); return true; } }); } return super.onCreateOptionsMenu(menu); } } public class MainActivity extends BaseActivity { } </code></pre> <p>This made obtain to manage common events handling such as LocationManager, onCreateOptionsMenu</p> <p>It works very good. But there is a little problem. onPause, onResume, onRestart I bind and unbind locationlistener event so Gps sometime doesn't have enough time to throw the locationchanged event becuase user can pass quickly between activity and there are bind and unbind events for GPS. So I made my LocationManager and LocationListener variables to static. Now it's perfect. In all activities I have the latest location of user. It's great! But the GPS is runing ON always. </p> <p>And here is the I want the thing how can I know the user send the application to back. I mean quit or pause it. </p> <p>P.S: onPause, onResume events run in all activities. I need something general about the whole application. </p>
    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.
 

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