last day (408 days later) » 
06:00 - 11:0011:00 - 14:00

6:46 AM
0
Q: GPS location is not updating in server onLocationChange

Prathibha KirthiIn GPS manager,I am getting the location update for every minutes in my Log. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0, this); This line did the trick. LOG: 02-03 11:32:33.045 6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732643 02-03 11:3...

 
Prathibha: i think you will need to call method for inserting data in onLocationChanged method which you are calling on Button click
 
ρяσѕρєя K...these both are in different activity...onLocationChanged is in GPSTracker class and Asyncktask which sends the data to server is in mainactivity class
 
Prathibha : then also separate logic for sending data to server in a separate class which you can use in both class's
 
And i read many post related to how to call AsynckTask from another activity..they are really very very complicated
ok...u mean create separate class which will just send data to server and invoke that class where ever needed
 
yes yes . and my suggestion is use IntentService for sending data to server instead of AsynckTask. because using IntentService first benefit is easy to use from any class just class startService method by sending data in intent. second benefit is if you close your app during sending data to server then IntentService automatically stop after completing task
Hi
 
6:47 AM
hello
do i need to change the entire code now
:(
 
no need to change entire code. just re-structure
first move all code related to sending data to server in a separate class
 
let me know if you have any issue in it
 
so by using intent will it run in background contnsly
hello pramod
 
no it will stop after sending data to server
 
6:51 AM
i will also suggest you that you should use intentservice instead of asynctask to send data to server as it is a more appropriate fit here i guess
 
ok...but i need to run the app continuously in background for 6-7 hours
every 15 min update the location in server
if i have to invoke only asynctask then how to do that
there??
@ρяσѕρєяK @PramodYadav
 
7:11 AM
class LongOperation extends AsyncTask<String, Void, Void>
{
******
}
this is in MainActivity
MainActivity.LongOperation();
or
new MainActivity.LongOperation().execute(Location);
not working
 
Hi Prathibha
 
hi @kevz
 
so is the problem solved?
 
I have read ur problem statement and I understood that u want to store GPS location update
rt?
 
7:21 AM
right
 
so this GPS location update where exactly u r saving it in local or on server?
 
on server
 
okay
I hope u know how to upload it on server
 
ys yes
it is uploading once
on button click
 
gotcha
so the code to upload GPS location is written in serviceclass
 
7:24 AM
no
 
then?
 
in mainactivity
i have 2 classes
gpstracker and main activity
 
okay goahead
 
in gpstracker i have all the stuff related to finding the location
 
okay
 
7:26 AM
public class GPSTracker extends Service implements LocationListener
{
}
 
gotcha
 
in mainactivity i have asynctask
class LongOperation extends AsyncTask<String, Void, Void>
{
******
}
this LongOperation will send data to server
 
okay
 
now,the headache is to call this method in onLocationChanged()
 
r u trying to send location update when the app is in background?
 
7:27 AM
so that it will update the changed location
exactly
that is my motto
 
I'll suggest a solution
 
Do u know how to call that LongOperation inside onLocationChanged()?
 
nooooooo
i dont know that
 
okay
how did u managed to send Location update once on button click
can u paste some code
 
7:32 AM
u may have a look at generated log cat here
 
I mean wt u did on button click?
let me check
 
on button click call to async task
new LongOperation().execute();
 
okay
 
within async task -- doInBackground()
call http
 
I want u to comment that line
 
7:34 AM
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost****
new LongOperation().execute(); this line
 
yep
 
did?
 
can I see ur onLocationChanged() code?
 
7:36 AM
giving link
 
okay
 
u need to comment this line locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,120000,0, this);
and write it in oncreate()
 
did u wrote it in onCreate()?
 
7:38 AM
it is there even at on create
 
may I see it?
 
send a link
 
I didn't used these MIN_DISTANCE_CHANGE_FOR_UPDATES & MIN_TIME_BW_UPDATES
so ignore the assigned values
 
gotcha
let me have a look
 
7:42 AM
ok
go ahead
 
1. Cut/paste Asyntask LongOperation from Activity to Service
let me know when u r done
done?
 
entire. class
?
 
show me the Activity class
 
entire asynctask class
 
yeah
 
7:46 AM
i need to cut n paste
 
or simple comment it in activity
done?
 
I assume the asyntask is in service now
now call it inside onLocationChanged()
 
done?
 
7:49 AM
done
 
ok now in activity inside button click just start this service
 
in main activity i have a edit text field
which accepts the mobile number
 
okay wt about it?
 
and send it to server along with lattitude and longitude
 
okay
 
7:51 AM
shall i use shared preference here
 
in ur service class haven't uve overriden onstartCommand()?
 
to get the mobile number inside service
 
yeah u can use it but the service class not complete
 
ok ok
and i dont have any onStart
onStatusChanged is there
 
where r the unimplemented method?
onStatusChanged is of LocationListener
 
7:54 AM
which method
yes yes
 
sorry u have one
 
where
i didn't get u kevz
 
its the @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
I want u to call get location inside oncreate() of service
override onCreate() inside service and call getLocation()
 
there is no onCreate in service
ok
 
rt u need to override it
 
7:56 AM
ok give a minute
 
and comment it
public GPSTracker(Context context)
{
this.mContext = context;
// getLocation();
}
done?
 
done
onCreate is never used...
 
okay about the Phno u can send it to service via Intent in putExtras
 
method doesn't overrirde from its super class
 
wt's the problem?
 
7:59 AM
removed @override
 
don't do that
 
@Override
public void onCreate(Bundle savedInstanceState)
{
getLocation();
}
when i give this,it is giving error at override
 
where's the call to super.oncreate()?
 
no where
 
put super.oncreate() b4 getlocation()
 
8:01 AM
ok ok
remove in main activity
keep it here
 
nope we r inside service
 
done?
 
yes kevz
 
override onStartCommand() inside service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
 
8:02 AM
hold on
 
and return START_STICKY
 
no need to pass savedInstanceState in onCreate
slowly kevzzz
 
nope its overridden mth
 
@Override
public void onCreate()
{
    super.onCreate();
    getLocation();
}
 
not of our service class
 
8:03 AM
is this ok
 
rt
 
hmmm
now onStartCommand
 
now override onStartCommand()
 
:)
and return START_STICKY
what is this
 
read about it later on developer it restart the service if killed by android os
hey can I get a break?
 
8:06 AM
:)
 
I'll be back in 35 min
 
just a minute huh
 
its Lunch break
 
where we need to return this start sticky
within onStartCommand
 
yep
 
8:07 AM
there is one return already
public int onStartCommand(Intent intent, int flags, int startId)
{
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
not possible na
 
remove the 1st return
 
I'll see u when I get back
 
sure
bye
i'll execute this mean while
we missed one part...call the service within main activity on button click
btnShowLocation.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent intent = new Intent(MainActivity.this, GPSTracker.class);
startService(intent);
}
added this
 
8:47 AM
i'M BACK
debug the code I'm sure the control shd be passed to Service
also u can simply send mobile number via intent.putExtras("Phno", mobile1.gettext().tostring());
and get it in onStartCommand(Intent intent. int flags, int startId)
intent.getStringExtras("Phno");
Comment getLocation inside onCreate() and add it inside onStartCommand()
 
9:21 AM
hey
 
Hi der
 
hi
went for lunch
 
cool
 
will this intent concept work in background
 
did u checked my above suggestion?
 
9:23 AM
yup
 
Intent will start service that runs in background
 
abou to implement
great
 
check it out and let me know
 
just a moment
 
take ur time
 
9:30 AM
debugging report..
control is not moving
 
s.o.p("Service started.."); write it in onCreate() of service
check if it gets printed in Logcat?
 
have u mentioned the service in Manifest?
 
lemme check
nope
 
do it 1st
 
9:40 AM
doing
<activity
android:name=".GPSTracker"
android:label="@string/title_activity_location" >
</activity>
error::
<service
android:name=".GPSTracker"
android:label="@string/title_activity_location" >
</service>
 
rt
 
GPSTracker doesn't have a default constructor
GPSTracker()
{
}
 
no need of constructor
 
if i add this in service its giving error for context
 
don't add it
remove that constructor
simply comment
 
9:48 AM
if i remove this error at manifest
 
wt error?
 
com.example.mylocation.GPSTracker' has no default constructor less... (Ctrl+F1)
this error
 
yeah u need to pass Context for locationlistener
uncomment that constructor
 
check if the service is started or not?
 
9:52 AM
by ignoring that error
 
Uncomment that constructor its needed
 
U might want to use application context inside service
so is the service started?
 
private final Context mContext;
now error is here
 
wt's the error?
 
9:55 AM
mContext might not have been initialized
 
private final Context mContext = null;
w8 a bit
plz comment that line
no need of context
also comment the GPStracker constructor
 
its used
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
mContext.startActivity(intent);
 
replace mcontext/context with GPSTracker.this
 
okay?
 
9:58 AM
yup
/* public GPSTracker(Context context)
{
this.mContext = context;
// getLocation();
}*/
commented this
added
GPSTracker()
{
}
 
rt
nope don't add any constructor
 
app crashed
stating
java.lang.RuntimeException: Unable to instantiate service com.example.mylocation.GPSTracker: java.lang.InstantiationException: can't instantiate class com.example.mylocation.GPSTracker; no empty constructor
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2406)
 
w8 let me check
 
is that the total error?
 
10:03 AM
if i add constructor-- access to constructor not allowed
no
many lines are there
 
forget constructor
 
check if u have added activity in manifest
 
i'll send u logcat
 
okay
 
10:03 AM
activity of main activity
 
ur MainActivity
 
yup added
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
 
now check
 
if error send me logcat
 
10:05 AM
crashed
 
send me logcat
 
send me updated service code
 
"Instead of getApplicationContext(), just use ActivityName.this"
this solution is available on net
i've used getApplicationContext in toast
 
yep but service slso runs on main thread
service itself is context
 
10:10 AM
ok
 
so no need of context
wt changes u made?
 
okk
in toast i replaced getApplicationContext
with
GPSTracker.this
 
allright run it
 
crashed
 
send me logcat
where's the error?
 
LET ME CHECK
 
hey comment the dialog part
service doesn't have UI
done?
 
okk
done
 
run it
 
10:21 AM
in process
service is called
and app crashed
null pointer exception
 
where?
the error?
 
can I have updated service code?
 
plz send
 
10:27 AM
doin
 
done?
 
u r not setting lat n log
okay
 
we did na
 
nope
w8 I'll correct the code
 
10:34 AM
within do in backgroung
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();

latti = Double.toString(latitude);
longi = Double.toString(longitude);
okk
 
that's not how u get lat n log
gps.getlatitude()
okay w8 a bit
 
change this
new LongOperation().execute();
to
new LongOperation(latitude, longitude).execute();

inside this class
class LongOperation extends AsyncTask<String, Void, Void>
{

// constructor
LongOperation(double latitude, double longitude){
latitude = latitude;
longitude = longitude;
}

} // end of class
comment this 2 lines
double latitude = gps.getLatitude(); double longitude = gps.getLongitude();
 
run it
done?
 
10:45 AM
yup
:)
app didn't crashed
checking db
 
okay
 
entry done
:)
 
glad it helped u
 
but mobile number it is not giving the newly entered one
used shared preference
is it because of that
 
maybe u haven't commit it properly
check it out
 
10:51 AM
did
@Override
public void onClick(View arg0)
{
mobile1 = (EditText) findViewById(R.id.EditText1);
mobile = mobile1.getText().toString();

Intent intent = new Intent(MainActivity.this, GPSTracker.class);
startService(intent);

if (mobile.length() == 10) {
prefer1 = getApplicationContext().getSharedPreferences("pref1", 0);
SharedPreferences.Editor editor = prefer1.edit();
editor.putString("mobile", mobile);
editor.commit();

// intent.putExtras("Phno", mobile1.gettext().tostring());
}
 
startservice after commit
 
06:00 - 11:0011:00 - 14:00

  last day (408 days later) »