I have three activity in my android application.
First is LoginActivity for input password and user id.
Seconed is MainActivity and last is UserInfo Activity.
In MainActivity, there is Runnable object for get GPS data.
It work every 10 minute.
UserInfo Activity has logout button.
When logout button is clicked, UserInfo Activity is finished and go to LoginActivity.
So I want exit GPS Runnable in Main Activity when logout button in clicked.
Is it possible to notify the MainActivity that the logout button has been clicked?
Or is it possible to access an Runnable object by UserInfo Activity or is there intent flag for exit handler?
Or is there any other way?
Here my logout code in UserInfo Activity
@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.btnLogout:
Intent intent = new Intent(UserActivity.this, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
break;
}
}
And get GPS Runnable in MainActivity.
private Handler gpsHandler;
private LocationManager locManager;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
gpsHandler = new Handler();
gpsHandler.postDelayed(gpsUpdate, 600000);
}
final Runnable gpsUpdate = new Runnable()
{
// https://stackoverflow.com/questions/6425611/android-run-a-task-periodically
@Override
public void run()
{
String result = "";
try
{
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
result = "Need Permission";
}
Location location = getLastKnownLocation();
if (location == null) // location update when fail get last location info
{
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 1, gpsLocationListener);
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 1, gpsLocationListener);
location = getLastKnownLocation();
if (location == null) // location get failed
{
result = "Can't get location info.";
}
}
if (location != null)
{
String provider = location.getProvider();
double longitude = location.getLongitude();
double latitude = location.getLatitude();
double altitude = location.getAltitude();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 1, gpsLocationListener);
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 1, gpsLocationListener);
result = ("provider : " + provider + "\n" +
"longitude : " + longitude + "\n" +
"atitude : " + latitude + "\n" +
"altitude : " + altitude);
}
}
catch(Exception e)
{
result = e.getMessage();
}
finally
{
gpsHandler.postDelayed(this, 600000);
updateGps(result);
}
}// run
}; // GPS UPDATE
private Location getLastKnownLocation()
{
//https://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null
List<String> providers = locManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers)
{
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return null;
}
Location l = locManager.getLastKnownLocation(provider);
if (l == null)
{
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy())
{
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
final LocationListener gpsLocationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
String provider = location.getProvider();
double longitude = location.getLongitude();
double latitude = location.getLatitude();
double altitude = location.getAltitude();
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
private void updateGps(String result)
{
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}