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.

4

2 に答える 2

8

私が見ている問題は、requestLocationUpdates()を呼び出した直後にonStart()でcurLocationをチェックしようとしていることです。LocationManager.requestLocationUpdates()は非同期です。つまり、onStart()が返されるまで、LocationListenerはコールバックされません。LocationListenerに、場所を受け取った後にUIを更新してもらいます。onStartでそれを実行しようとしないでください。

于 2009-11-27T18:56:34.883 に答える
3

エミュレータまたはデバイスでこの問題が発生していますか? エミュレーターの場合は、必ず DDMS を使用して GPS 位置を設定してください。しかし、最も簡単な方法は、デバイスでコードを実行して、GPS の更新を取得できるかどうかを確認することです。

于 2009-11-26T06:46:07.773 に答える