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. driving/walking directions on mapview and clearing of application(map) cache

Hi guys,

May I ask if anyone know how to get the driving/walking directions on
google maps (if supported) on mapview? so far I can only calculate
linear direction...

Also anyone know whats the command to clear application cache? I can
only find the permission to do it when i google for it.. i have no
idea how to use it =(

public static final String DELETE_CACHE_FILES
Allows an application to delete cache files.
Constant Value: "android.permission.DELETE_CACHE_FILES"


--~--~---------~--~----~------------~-------~--~----~

2. Intent and URI conversion problem (intent.toURI, Uri.parse)

we can convert from Intent to URI and from URI to Intent.
from Intent to URI string
Intent intent1 = new Intent ();
String uriString = intent1.toURI ();

from URI string to Intent
Uri uri = Uri.parse (uriString);
Intent intent2 = new Intent ();
intent2.setData (uri)

after conversion, intent1 and inten2 should be same.
for example
        Intent intent1 = new Intent ();
        intent1.addCategory (Intent.CATEGORY_HOME);
        intent1.addCategory (Intent.CATEGORY_LAUNCHER);
        Log.v ("", "Intent1 = " + intent1);
        if (intent1.hasCategory (Intent.CATEGORY_HOME))
            Log.v ("Intent1", "#1 hasCategory TRUE");
        else
            Log.v ("Intent1", "#1 hasCategory FALSE");

        String str = intent1.toURI ();
        Uri uri = Uri.parse (str);
        Intent intent2 = new Intent ();
        intent2.setData (uri);
        Log.v ("", "Intent2 = " + intent2);
        if (intent2.hasCategory (Intent.CATEGORY_HOME))
            Log.v ("Intent2", "#2 hasCategory TRUE");
        else
            Log.v ("Intent2", "#2 hasCategory FALSE");
-------------- result --------------
Intent1 =
{ categories=android.intent.category.HOME,android.intent.category.LAUNCHER} }
 #1 hasCategory TRUE

Intent2
{ 
data=#Intent;category=android.intent.category.HOME;category=android.intent.category.LAUNCHER;end
 }
 #2 hasCategory FALSE

In this sample code, Intent2 is a conversion result from Intent1. If
you run this sample code, Intent1 result and Intent2 result are
differnt. because Intent1 and Intent2 have a same
Intent.CATEGORY_HOEM, the result should be Intent1 and Intent2 is TRUE
(intent.hasCategory).

I think this is bug. and the hint is Intent1 and Intent2 's toString
result is different.
Intent1 =
{categories=android.intent.category.HOME,android.intent.category.LAUNCHER}
Intent2 =
{ 
data=#Intent;category=android.intent.category.HOME;category=android.intent.category.LAUNCHER;end
 }

I don't know how to convert Intent to URI(String) and convert URI
(String)
 to Intent. I want to save Intent to DB and read Intent from DB.
please help me!



--~--~---------~--~----~------------~-------~--~----~

3. ActivityGroup , Alerts and Configuration changes

4. Problems with creating a Hello World Application for Android

5. Non-Ti Wifi driver

6. How to put a breakpoint in code used by Phone app?

7. WifiLayer 6s timer and wpa_supplicant 5s timer sometimes confilct