2012-10-04 2 views
1

나는 위치의 업데이트를 시작하고 2 분 를 대기 위치에 대한 폴링 서비스를 만들려고하고 난 내 에뮬레이터를 사용하여 위치를 보내려고 할 때 다음 업데이트 안드로이드 폴링 위치 서비스를 작성하는 데 실패

을 제거하고 내 locationListener가 수신하지 않는 것 같습니다.

내가 스레드에서 만든 루퍼에 대한 처리기를 구현하지 않았기 때문에 그것이 무엇입니까?

또는 내 스레드가 너무 잠 때문에 내가 어떤 위치

package android.co.in; 

import android.graphics.Canvas; 
import android.os.Looper; 
import android.view.SurfaceHolder; 

public class customThread extends Thread { 

boolean stop; 
boolean run; 
customThread() 
{ 
    BounceLogger.logIt(this, "Constructor()"); 
    stop=false; 
    run=true; 
} 
@Override 
public void run() 
{ 
    Looper.prepare(); 
    BounceLogger.logIt(this, "run()"); 
    while(!stop) 
    { 
     while(run) 
     { 
     updateThread(); 
     } 

     try 
     { 
      wait(); 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
    Looper.loop(); 
} 

//derived class should overide this function 
public void updateThread() 
{ 


} 


public void runThread() 
{ 
    start(); 
} 

public void stopThread() 
{ 
    stop=true; 
} 

public void pauseThread() 
{ 
    run=false; 
    BounceLogger.logIt(this,"calling wait on worker thread"); 
} 

public void resumeThread() 
{ 
    BounceLogger.logIt(this,"calling notify on worker thread"); 
    run=true; 
    notify();  
} 


} 

//responsible for all location related queries 
public class UserLocationManager extends customThread{ 

boolean locationFound; 

LocationSelector locationSelector; 

UserLocationManager(BuddiesAroundActivity activity) 
{ 
    super(); 
    locationFound=false; 
    locationSelector=LocationSelector.getLocationSelector(activity); 
} 

Location GetUserLocation() 
{ 
    queryUserLocation(); 
    return locationSelector.getLastKnownLocation(); 
} 

@Override 
public void updateThread() 
{ 
    locationSelector.startListening(); 
    try { 
     Thread.sleep(200000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    locationSelector.stopListening(); 
} 

void queryUserLocation() 
{  
    runThread();  
} 


} 

package android.co.in; 

import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

public class LocationSelector 
{ 

private static LocationSelector locationSelector=null; 
private static final int TWO_MINUTES = 1000 * 60 * 2; 
LocationManager locationManager; 
LocationListener locationListener; 
Location lastKnownLocation; 

LocationSelector() 
{ 
    Intialize(); 
} 

LocationSelector(BuddiesAroundActivity activity) 
{ 
    Intialize(); 
    locationManager= 
     (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE); 
} 

static LocationSelector getLocationSelector(BuddiesAroundActivity activity) 
{ 
    if(locationSelector==null) 
     locationSelector = new LocationSelector(activity); 

    return locationSelector;  
} 

void startListening() 
{ 
    if(locationManager!=null) 
    { 
     BounceLogger.logIt(this, "started listening on location updates"); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100,  locationListener); 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,100, locationListener); 
    } 
} 

public void stopListening() 
{ 
    BounceLogger.logIt(this, "stopped listening on location updates"); 
    locationManager.removeUpdates(locationListener); 
} 


private void Intialize() 
{ 
    lastKnownLocation=null; 
    // TODO Auto-generated method stub 
    locationListener=new LocationListener() { 
     public void onLocationChanged(Location current) 
     { 
      // Called when a new location is found by the network location  provider. 
      BounceLogger.logIt(this,"recived a location"+current.getLatitude()+":"+current.getLongitude()); 
      if(lastKnownLocation==null) 
      { 
       lastKnownLocation=current; 
      } 
      getBestLocation(lastKnownLocation,current); 

     } 

     public void onStatusChanged(String provider, int status, Bundle extras) {} 

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 

     }; 

} 

private float getDistanceBetweenLocations(Location a,Location b) 
{ 
    float distance =a.distanceTo(b);  
    return distance;  
} 

private double getAngleBetweenLocations(Location origin,Location destination) 
{ 
    double angle=0.0f; 
    double longDiff; 
    double latDiff; 

    longDiff=destination.getLongitude()-origin.getLongitude(); 
    latDiff=destination.getLatitude()-origin.getLatitude(); 

    angle=Math.atan2(longDiff,latDiff);   
    return angle; 
} 

Location getLastKnownLocation() 
{ 
    return lastKnownLocation; 
} 

Location getBestLocation(Location old,Location current) 
{ 
    if(old ==null) 
     return current; 

    //check time 
    long timeDelta = current.getTime() - old.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    int useCurrentLocationByTime=0; 
    if(isSignificantlyNewer) 
    { 
     useCurrentLocationByTime++; 
    } 

    //check for accuracy 
    int useCurrentLocationByAccuracy=0; 
    if(old.getAccuracy() < current.getAccuracy()) 
    { 
     useCurrentLocationByAccuracy++; 
    } 

    //check for provider this is blunt but u might want give priority to providers and then decide 
    int useCurrentLocationByProvider=0; 
    if(old.getProvider().equals(current.getProvider())) 
    { 
     useCurrentLocationByProvider++; 
    } 

    int points=useCurrentLocationByTime+useCurrentLocationByAccuracy+useCurrentLocationByProvider; 

    if(points > 1.5) 
    { 
     return current;  
    } 

    return old;  
} 

} 당신의 updateThread에서

답변

0

당신이 위치에 대한 요청을 시작하고 다음 200 초 동안 스레드 TP 수면을 넣어를받지 않습니다, 새로운 위치를받을 수 없게됩니다. 스레드가 깨어나 자마자 위치 업데이트 요청을 제거합니다.

Location Listner가 다른 스레드에서 실행되는 방식으로 코드를 다시 디자인해야합니다. 또는 더 나은 방법으로 handler.post() 메소드를 사용하여 200 초 후에 위치 수신기를 정지시키는 실행 가능 파일을 실행하고 이를 위해 쓰레드를 사용하지 않아야합니다.

--EDITED--

당신은 같은 것을 할 sould :

Handler handler = new Handler(); 
startLocationListener(); //in this method you only add the listener for onLocationsChange(). No threads needed. 
handler.postDelayed(rStopLocationListener, 200000); //the runnable rStopLocationListener will run in 200 seconds 

//define rStopLocationListener 
private Runnable rStopLocationListener = new Runnable() { 
    public void run() { 
      stopLocationListener(); //in this method you only remove the listener for onLocationsChange(). No threads needed. 
    } 
}; 
+0

감사 응답 r에. 나는 더 나은 접근해야 핸들을 통해 그것을 구현합니다 :) –

+0

도와 줘서 다행. 코드 예제가 필요한 경우 알려주십시오. – Luis

+0

네, 주셔서 감사합니다 !!!. 이 구현에 문제가 있는지 확인하십시오 :) –

0

패키지 android.co.in을;

import android.graphics.Canvas; 
import android.os.Looper; 
import android.view.SurfaceHolder; 

public class customThread extends Thread { 

boolean stop; 
boolean run; 
customThread() 
{ 
    BounceLogger.logIt(this, "Constructor()"); 
    stop=false; 
    run=true; 
} 
    @Override 
    public void run() 
    { 
    //Looper.prepare(); 
    BounceLogger.logIt(this, "run()"); 
    while(!stop) 
    { 
     while(run) 
     { 
     updateThread(); 
     } 

     try 
     { 
      wait(); 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
    //Looper.loop(); 
    } 

    //derived class should overide this function 
    public void updateThread() 
    { 


    } 


    public void runThread() 
    { 
    start(); 
    } 

    public void stopThread() 
{ 
    stop=true; 
} 

    public void pauseThread() 
{ 
    run=false; 
    BounceLogger.logIt(this,"calling wait on worker thread"); 
} 

    public void resumeThread() 
{ 
    BounceLogger.logIt(this,"calling notify on worker thread"); 
    run=true; 
    notify();  
} 


    } 

    class customLooperThread extends Thread { 

boolean stop; 
boolean run; 
customLooperThread() 
{ 
    BounceLogger.logIt(this, "Constructor()"); 
    stop=false; 
    run=true; 
} 
    @Override 
    public void run() 
    { 
    Looper.prepare(); 
    BounceLogger.logIt(this, "run()"); 
    while(!stop) 
    { 
     while(run) 
     { 
     updateThread(); 
     } 

     try 
     { 
      wait(); 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
    Looper.loop(); 
    } 

    //derived class should overide this function 
    public void updateThread() 
    { 


    } 


public void runThread() 
{ 
    start(); 
} 

public void stopThread() 
{ 
    stop=true; 
} 

public void pauseThread() 
{ 
    run=false; 
    BounceLogger.logIt(this,"calling wait on worker thread"); 
} 

    public void resumeThread() 
{ 
    BounceLogger.logIt(this,"calling notify on worker thread"); 
    run=true; 
    notify();  
} 


    } 




    //responsible for all location related queries 
    public class UserLocationManager extends customLooperThread{ 

boolean locationFound; 

LocationSelector locationSelector; 
Handler h; 

UserLocationManager(BuddiesAroundActivity activity,Handler uiHandler) 
{ 
    super(); 
    locationFound=false; 
    locationSelector=LocationSelector.getLocationSelector(activity); 

} 

Location GetUserLocation() 
{ 
    queryUserLocation(); 
    return locationSelector.getLastKnownLocation(); 
} 

@Override 
public void updateThread() 
{ 
    locationSelector.startListening(); 
    h= new Handler(); 
    h.postAtTime(locationSelector, 20000); 
    try { 
     sleep(20000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    locationSelector.stopListening(); 
} 

void queryUserLocation() 
{  
    runThread();  
} 


    } 



    public class LocationSelector extends customThread 
    { 

private static LocationSelector locationSelector=null; 
private static final int TWO_MINUTES = 1000 * 60 * 2; 
LocationManager locationManager; 
LocationListener locationListener; 
Location lastKnownLocation; 

LocationSelector() 
{ 
    Intialize(); 
} 

LocationSelector(BuddiesAroundActivity activity) 
{ 
    Intialize(); 
    locationManager= 
       (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE); 
} 

static LocationSelector getLocationSelector(BuddiesAroundActivity activity) 
{ 
    if(locationSelector==null) 
     locationSelector = new LocationSelector(activity); 

    return locationSelector;  
} 

void startListening() 
{ 
    if(locationManager!=null) 
    { 
     BounceLogger.logIt(this, "started listening on location updates"); 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100, 
      locationListener); 

     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,100,     
     locationListener); 
    } 
} 

public void stopListening() 
{ 
    BounceLogger.logIt(this, "stopped listening on location updates"); 
    locationManager.removeUpdates(locationListener); 
} 


@Override 
public void updateThread() 
{ 
BounceLogger.logIt(this, "updateThread"); 
} 


private void Intialize() 
{ 
    lastKnownLocation=null; 
    // TODO Auto-generated method stub 
    locationListener=new LocationListener() { 
     public void onLocationChanged(Location current) 
     { 
      // Called when a new location is found by the network location provider. 
      BounceLogger.logIt(this,"recived a location"+current.getLatitude()+":"+current.getLongitude()); 
      if(lastKnownLocation==null) 
      { 
       lastKnownLocation=current; 
      } 
      getBestLocation(lastKnownLocation,current); 

     } 

     public void onStatusChanged(String provider, int status, Bundle extras) {} 

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 

     }; 

} 

private float getDistanceBetweenLocations(Location a,Location b) 
{ 
    float distance =a.distanceTo(b);  
    return distance;  
} 

private double getAngleBetweenLocations(Location origin,Location destination) 
{ 
    double angle=0.0f; 
    double longDiff; 
    double latDiff; 

    longDiff=destination.getLongitude()-origin.getLongitude(); 
    latDiff=destination.getLatitude()-origin.getLatitude(); 

    angle=Math.atan2(longDiff,latDiff);   
    return angle; 
} 

Location getLastKnownLocation() 
{ 
    return lastKnownLocation; 
} 

Location getBestLocation(Location old,Location current) 
{ 
    if(old ==null) 
     return current; 

    //check time 
    long timeDelta = current.getTime() - old.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    int useCurrentLocationByTime=0; 
    if(isSignificantlyNewer) 
    { 
     useCurrentLocationByTime++; 
    } 

    //check for accuracy 
    int useCurrentLocationByAccuracy=0; 
    if(old.getAccuracy() < current.getAccuracy()) 
    { 
     useCurrentLocationByAccuracy++; 
    } 

    //check for provider this is blunt but u might want give priority to providers and then decide 
    int useCurrentLocationByProvider=0; 
    if(old.getProvider().equals(current.getProvider())) 
    { 
     useCurrentLocationByProvider++; 
    } 

    int points=useCurrentLocationByTime+useCurrentLocationByAccuracy+useCurrentLocationByProvider; 

    if(points > 1.5) 
    { 
     return current;  
    } 

    return old;  
} 

} U 용

+0

내 편집 된 게시물을 참조하십시오. – Luis

관련 문제