Problem in GPS while running in timer

by Nemat » Wed, 11 Nov 2009 14:01:40 GMT


Sponsored Links
 i frinds......

I am presently working on GPS.I have to start GPS periodically.I mean
I want GPS to start after a particular intervel,get Location value and
then stop taking location updates.For this,I m calling
requestLocationUpdates( , , ) method in a timer.Then in
onLocationChanged(), I stop taking location by using removeUpdates
().Then after particular intervel ,it again start taking values,then
when location changes ,it stops, and so on......

Here is my code:
TimerTask IntervelTimer = new TimerTask()
{
public void run()
{
Log.d
("startLocationUpdate","startLocationUpdate");
startLocationUpdate();

}
};
Timer timer2 = new Timer();


try{
timer2.schedule(IntervelTimer, 1*60 * 1000, 1*60 * 1000);
}
catch(Exception e)
{
Log.e("Error",e.toString());
}

public void startLocationUpdate()
{
System.out.println("startLocationUpdate()");
if(locationListener==null)
{
Looper.prepare();

locationListener = new MyLocationListener();
try{
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}
catch(Exception e)
{
Log.e("error",e.toString());
}
Looper.loop();
Looper.myLooper().quit();
}
}

private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
System.out.println("GetLocation");
if (loc != null) {
StoreGPSlocation(String.valueOf
(loc.getLatitude()),String.valueOf(loc.getLongitude()));
stopLocationUpdate();
}
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}

public void stopLocationUpdate()
{
LocationManager lm = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
if (lm != null)

{
if (locationListener != null)

{

lm.removeUpdates(locationListener);
locationListener= null;

}



lm = null;

}



}

Now the problem is:It starts fine for the first tim



Problem in GPS while running in timer

by Nemat » Wed, 11 Nov 2009 14:01:46 GMT


 i frinds......

I am presently working on GPS.I have to start GPS periodically.I mean
I want GPS to start after a particular intervel,get Location value and
then stop taking location updates.For this,I m calling
requestLocationUpdates( , , ) method in a timer.Then in
onLocationChanged(), I stop taking location by using removeUpdates
().Then after particular intervel ,it again start taking values,then
when location changes ,it stops, and so on......

Here is my code:
TimerTask IntervelTimer = new TimerTask()
{
public void run()
{
Log.d
("startLocationUpdate","startLocationUpdate");
startLocationUpdate();

}
};
Timer timer2 = new Timer();


try{
timer2.schedule(IntervelTimer, 1*60 * 1000, 1*60 * 1000);
}
catch(Exception e)
{
Log.e("Error",e.toString());
}

public void startLocationUpdate()
{
System.out.println("startLocationUpdate()");
if(locationListener==null)
{
Looper.prepare();

locationListener = new MyLocationListener();
try{
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}
catch(Exception e)
{
Log.e("error",e.toString());
}
Looper.loop();
Looper.myLooper().quit();
}
}

private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
System.out.println("GetLocation");
if (loc != null) {
StoreGPSlocation(String.valueOf
(loc.getLatitude()),String.valueOf(loc.getLongitude()));
stopLocationUpdate();
}
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}

public void stopLocationUpdate()
{
LocationManager lm = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
if (lm != null)

{
if (locationListener != null)

{

lm.removeUpdates(locationListener);
locationListener= null;

}



lm = null;

}



}

Now the problem is:It starts fine for the first tim


Sponsored Links


Problem in GPS while running in timer

by 褰偟 » Mon, 16 Nov 2009 12:47:25 GMT


 i, Nemat

I think your problem is that you call requestLocationUpdates in the
TimerTask thread.

As the
document<http://androidappdocs.appspot.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,
long, float, android.location.LocationListener)> says, The calling thread
must be a
Looper<file:///E:/SDK/android-sdk-windows-1.6_r1/docs/reference/android/os/Looper.html>thread
such as the main thread of the calling Activity.

2009/11/11 Nemat <nemate...@gmail.com>

>



Other Threads

1. Iseng quadrant si ns :)

Waaaaaah ajib bener.
Nexus S ane baru di root nih, belum custom. Quadrantnya masih 1700an

Sent from  Iphe 4






-- 
"

2. Berbagi : AutoCAD WS for Android Available Now

https://market.android.com/details?id=com.autodesk.autocadws
DESCRIPTION
Experience the freedom of taking your designs with you wherever you go.
AutoCAD WS is a mobile CAD application that gives you the freedom and
flexibility to view, edit, and share your DWGfiles on your Android
phone or tablet. AutoCAD WS mobile app offers a simplified, intuitive
set of viewing, editing, and markup tools so you can work on your
designs while you are on the go. Open drawings from email attachments,
sync your files from the web, or upload drawings directly from AutoCAD
software. Drawings can also be saved locally so you can work in the
field without an Internet connection.
*Requires Android 2.1 or above. Recommended devices include: 1GHz
processor, 512MB RAM.


-- 

foeng
bdg

-- 
"

3. OOT : Detik Dot Com

4. Fwd: Handle Multi Screen

5. (repost) WTS: Nook Color (ebook reader/tablet ecclair)

6. Lacak posisi hp

7. AlarmManager woes