When the application open I want to send the latitude and longitude but when the app opens its take some time to fetch the lat and long. I'm calling the service class in the onResume method using handler,once I got latitude and longitude I want to stop the handler because I want to make a API rest call but handler is not stopped its keep on running even though I stopped.Please help me.
this is code:
package com.getspot.getspot.managerpart;
import android.Manifest;
import android.app.Activity;
public class ManagerHomePage extends AppCompatActivity {
private Handler mHandler1 = new Handler();
private final int REFRESH_RATE1 = 10000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this
, getString(R.string.access_token));
setContentView(R.layout.activity_home);
ctx=this;
intent1 = new Intent(ctx,
MyLocationService1.class);
startService(intent1);
}
@Override
protected void onResume() {
super.onResume();
mHandler1.removeCallbacks(startTimer1);
mHandler1.postDelayed(startTimer1, 0);
}
private Runnable startTimer1 = new Runnable() {
public void run() {
ctx.startService(new Intent(ctx,MyLocationService1.class));
gs_sharepref_latlong = ctx.getSharedPreferences("myprefer", Context.MODE_PRIVATE);
gs_str_latitude = gs_sharepref_latlong.getString("Lat", null);
gs_str_Longitude = gs_sharepref_latlong.getString("Long", null);
// GS_UserUpdateLocation(ctx,gs_str_Latitude1,gs_str_Longitude1);
gs_str_Latlong=gs_str_latitude+","+gs_str_Longitude;
Log.i("AA","shared"+gs_sharepref_latlong.getString("Lat", null));
if(gs_sharepref_latlong.getString("Lat", null)!=null)
{
mHandler1.removeCallbacks(startTimer1);
Toast.makeText(ctx, "if--LATLONG-**-" + gs_sharepref_latlong.getString("Lat", null), Toast.LENGTH_LONG).show();
callmethod();
Log.i("AA","iff latitude"+gs_str_latitude);
}
else
{
Log.i("AA","elsee--lat"+gs_str_latitude);
Toast.makeText(ctx, "else--LATLONG--"+gs_sharepref_latlong.getString("Lat", null) , Toast.LENGTH_LONG).show();
}
mHandler1.postDelayed(this,REFRESH_RATE1);
}
};
private void callmethod() {
if(gs_str_latitude!=null && !gs_str_latitude.equalsIgnoreCase("") && !gs_str_latitude.isEmpty())
{
mHandler1.removeCallbacks(startTimer1);
// GS_User_LoggedStatus(ctx,gs_var_userid);
Log.i("AA","calmethod--lat"+gs_str_latitude);
}
}
}
Note: In if statement I tried to stopped the handler but its not stopping even though I'm calling the one method ,in that method also I'm stopped the handler but its not wokring please help me.