在Android小部件中实现位置监听器时出现问题

4
我正在开发一个小部件,它将获取当前 GPS 位置并将该值传递给远程 PHP 页面以获取信息并在小部件中显示。这就是我想要做的事情。
我在实现 AppWidget 的 Location Listener 时遇到了问题。它没有更新当前位置,并且显示初始小部件,即 “加载小部件” (这里是我放置的文本)。 我们可以为 AppWidgetProvider 实现 Location Listener 吗? 或者 你能建议我获取 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.

2个回答

8
我看到的问题是,您试图在调用requestLocationUpdates()之后立即在onStart()中检查curLocation。LocationManager.requestLocationUpdates()是异步的,这意味着您的LocationListener直到onStart()返回后才会被回调。让您的LocationListener在接收到位置后更新UI,不要试图在onStart()中完成此操作。

嗨Bryan, 你说得对。我遇到了一个问题,就是我给AppWidget设置的时间间隔(20秒)和位置监听器(2秒)不同步。我将AppWidget的时间间隔设置为1分钟,位置监听器的时间间隔设置为20秒。现在一切都正常了...非常感谢你的指导... - Raghu
很高兴能够帮助。如果您有时间,也许可以将此答案标记为正确答案。 - Bryan Bedard

3

您遇到的问题是在模拟器上还是设备上?如果是在模拟器上,请确保使用DDMS设置GPS位置。但最简单的方法可能是在设备上运行代码,以查看是否通过此方式获取GPS更新。


嗨,Mike, 我在我的HTC Hero上进行了测试。但是没有成功。我的代码有什么问题吗? 或者你能否建议我其他方法来在小部件中获取GPS位置?感谢您的快速回复... - Raghu
我很乐意帮忙,但我对小部件不是很熟悉。还有一件事我忘了提醒,就是确保你带着设备在室外走动——我过去曾经因为设备无法在室内获取GPS信号而无法收到GPS更新信息,导致代码出现问题。 - emmby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接