2009-11-26 97 views
3

我正在使用一个Widget,它将获取当前GPS位置并将此值传递给远程PHP页面以获取信息并将其显示在Widget中。这是我想要做的。在android appwidget中实现位置监听器时出现问题

我在为appWidget实现Location Listener时遇到问题。它不是用当前位置进行更新,而是显示最初的小部件,即“加载小部件”(这里我把这个文本)

我们可以为AppWidgetProvider实现位置监听器吗?

你能否建议我在App Widget中获取GPS位置的可能解决方案?

我把所有必要的权限放在清单文件中。
这里是我的代码片段:

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

嗨迈克, 我在我的HTC英雄测试。但没有运气。我的代码有问题吗? 还是你可以建议我任何其他方式来获得gps位置的Widget? 感谢您的快速反应... – Raghu 2009-11-26 11:27:32

+0

我很乐意提供帮助,但我对小部件不是很熟悉。我忘记提及的一件事就是确保你使用设备在外面走动 - 过去我一直在努力处理代码,只是意识到设备无法在室内获得GPS定位,因此不会发送给我GPS更新。 – emmby 2009-11-26 17:22:21

8

我看到的问题是您正在尝试在调用requestLocationUpdates()后立即检查onStart()中的curLocation。 LocationManager.requestLocationUpdates()是异步的,这意味着在onStart()返回之前,您的LocationListener不会被回调。让LocationListener在接收到位置后更新UI,请勿尝试在onStart中执行此操作。

+0

嗨布莱恩, 你是对的。我遇到了我给AppWidget(20秒)和位置侦听器(2秒)提供的时间间隔的问题。然后两者不会相互同步。我将AppWidget的时间间隔设置为1分钟,将位置监听器设置为20秒。现在它工作正常... 非常感谢您的指导... – Raghu 2009-11-30 03:58:08

+0

乐于帮助。如果你有一分钟​​,也许将这个答案标记为正确答案。 – 2009-11-30 16:46:48