2009-11-26 4 views
3

위젯에서 현재 GPS 위치를 얻고이 값을 원격 PHP 페이지에 전달하여 정보를 얻고 위젯에 표시합니다. 이것이 내가하려는 일이다.안드로이드 appwidget에 위치 수신기를 구현하는 동안 문제가 발생했습니다.

appWidget의 위치 수신기를 구현하는 중 문제가 발생했습니다. 현재 위치로 업데이트하지 않고 초기 위젯 즉 "위젯로드 중"을 표시합니다 (여기에서이 텍스트를 넣습니다)

AppWidgetProvider에 대한 위치 수신기를 구현할 수 있습니까?

App 위젯에서 GPS 위치를 확인하는 방법을 제안 할 수 있습니까?

필요한 모든 권한을 Manifest 파일에 넣었습니다. 사전에

public class test extends AppWidgetProvider { 
static LocationManager locMan; 
static Location curLocation; 
static Boolean locationChanged; 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) { 
    // To prevent any ANR timeouts, we perform the update in a service 
    context.startService(new Intent(context, UpdateService.class)); 
} 

public static class UpdateService extends Service { 
    // GPS Listener Class 
    LocationListener gpsListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      // Log.w("GPS", "Started"); 
      if (curLocation == null) { 
       curLocation = location; 
       locationChanged = true; 
      } 

      if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()) 
       locationChanged = false; 
      else 
       locationChanged = true; 

      curLocation = location; 

      if (locationChanged) 
       locMan.removeUpdates(gpsListener); 

     } 

     public void onProviderDisabled(String provider) { 

     } 

     public void onProviderEnabled(String provider) { 
      // Log.w("GPS", "Location changed", null); 
     } 

     public void onStatusChanged(String provider, int status, 
       Bundle extras) { 
      if (status == 0)// UnAvailable 
      { 

      } else if (status == 1)// Trying to Connect 
      { 

      } else if (status == 2) {// Available 

      } 
     } 

    }; 

    // In service start method, I am registering for GPS Updates 
    @Override 
    public void onStart(Intent intent, int startId) { 

     locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener); 
     } else { 
      this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS")); 
     } 

     if (curLocation != null) { 
      double lat = curLocation.getLatitude(); 
      double lng = curLocation.getLongitude(); 
      Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show(); 

     } 
     // Build the widget update for today 
     RemoteViews updateViews = buildUpdate(this); 

     // Push update for this widget to the home screen 
     ComponentName thisWidget = new ComponentName(this,InKakinadaWidget.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(thisWidget, updateViews); 

    } 

    public RemoteViews buildUpdate(Context context) { 
     // Here I am updating the remoteview 
     return updateViews; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // We don't need to bind to this service 
     return null; 
    } 

} 
} 

감사합니다 ... 감사와

, Raghavendra K.

답변

3

에뮬레이터 또는 장치에서이 문제가 있습니까 : 여기
내 코드인가? 에뮬레이터에서 DDMS를 사용하여 GPS 위치를 설정했는지 확인하십시오. 하지만 가장 쉬운 방법은 장치에서 코드를 실행하여 GPS 업데이트를받을 수 있는지 확인하는 것입니다.

+0

안녕하세요 Mike, 나는 내 HTC Hero에서 테스트를 마쳤습니다. 그러나 운이 없다. 내 코드에 문제가 있습니까? 또는 위젯에서 GPS 위치를 얻는 다른 방법을 제안 할 수 있습니까? 당신의 빠른 respnose 주셔서 감사합니다 ... – Raghu

+0

내가 도와주고 싶지만 위젯에 익숙하지 않은데. 내가 언급 한 것을 잊어 버리는 한 가지는 장치를 가지고 바깥을 돌아 다니는 것을 확인하는 것입니다. 나는 과거의 코드로 어려움을 겪었습니다. 실내에서만 GPS를 사용할 수 없으므로 나를 보내지 않았습니다. GPS 업데이트. – emmby

8

내가보기에 문제는 requestLocationUpdates()를 호출 한 직후에 onStart()에서 curLocation을 확인하려고한다는 것입니다. LocationManager.requestLocationUpdates()는 비동기식입니다. 즉, onStart()가 반환 될 때까지 LocationListener가 다시 호출되지 않습니다. LocationListener가 위치를 수신 한 후에 UI를 업데이트하도록하십시오. onStart에서 시도하고하지 마십시오.

+0

안녕 Bryan, 맞습니다. 나는 AppWidget (20 초)과 Location Listener (2 초)에 주어진 Time Intervals의 문제에 직면했다. 둘 다 서로 동기화되지 않습니다. AppWidget의 시간 간격을 1 분으로, 위치 수신기의 시간 간격을 20 초로 설정했습니다. 이제는 잘 작동하고 있습니다 ... 지도 주셔서 대단히 감사합니다 ... – Raghu

+0

도와 드리겠습니다. 잠시 시간이 있으면이 대답을 올바른 것으로 표시하십시오. –

관련 문제