3

我下载了一个应用程序,它给出了用户的纬度和经度,并从 GPS 获取信息。下面的代码在那里,它在 Android 4.0.3 上运行良好,但在 Android 2.3.3 上它不起作用。我在模拟器和设备上都试过了。在 Android 4.0.3 设备和模拟器上,完美运行,但在 Android 2.3.3 设备和模拟器上不起作用。这是我从网上下载的项目链接。 http://www.javacodegeeks.com/2010/09/android-location-based-services.html

感谢您的帮助。

package com.javacodegeeks.android.lbs;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LbsGeocodingActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    protected LocationManager locationManager;

    protected Button retrieveLocationButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showCurrentLocation();
            }
        });        

    }    

    protected void showCurrentLocation() {

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(LbsGeocodingActivity.this, message,
                    Toast.LENGTH_LONG).show();
        }

    }   

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }

    }

}

这是将坐标发送到我的 2.3.3 模拟器后的 logcat

06-19 10:04:27.614: I/ActivityManager(60): Displayed com.javacodegeeks.android.lbs/.LbsGeocodingActivity: +1s18ms
06-19 10:04:56.424: I/DEBUG(30): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-19 10:04:56.424: I/DEBUG(30): Build fingerprint: 'generic/sdk/generic:2.3.3/GRI34/101070:eng/test-keys'
06-19 10:04:56.434: I/DEBUG(30): pid: 60, tid: 161  >>> system_server <<<
06-19 10:04:56.434: I/DEBUG(30): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000000
06-19 10:04:56.434: I/DEBUG(30):  r0 00000000  r1 40643f90  r2 41adff0c  r3 4689fc74
06-19 10:04:56.434: I/DEBUG(30):  r4 00000138  r5 00000000  r6 40643f90  r7 41adff0c
06-19 10:04:56.434: I/DEBUG(30):  r8 80701321  r9 80702240  10 00100000  fp 00000001
06-19 10:04:56.434: I/DEBUG(30):  ip ae20e7ec  sp 4689fc60  lr ae20acd7  pc ae207dfe  cpsr 00000030
06-19 10:04:56.694: I/DEBUG(30):          #00  pc 00007dfe  /system/lib/libandroid_servers.so
06-19 10:04:56.694: I/DEBUG(30):          #01  pc 0000acd2  /system/lib/libandroid_servers.so
06-19 10:04:56.704: I/DEBUG(30):          #02  pc 000012ca  /system/lib/hw/gps.goldfish.so
06-19 10:04:56.704: I/DEBUG(30):          #03  pc 000014ae  /system/lib/hw/gps.goldfish.so
06-19 10:04:56.714: I/DEBUG(30):          #04  pc 00011a7c  /system/lib/libc.so
06-19 10:04:56.714: I/DEBUG(30):          #05  pc 00011640  /system/lib/libc.so
06-19 10:04:56.714: I/DEBUG(30): code around pc:
06-19 10:04:56.714: I/DEBUG(30): ae207ddc ab04b082 9301cb04 6f646804 b00247a0 
06-19 10:04:56.724: I/DEBUG(30): ae207dec bc08bc10 4718b002 b510b40c ab04b082 
06-19 10:04:56.734: I/DEBUG(30): ae207dfc 6804cb04 34f89301 47a06824 bc10b002 
06-19 10:04:56.734: I/DEBUG(30): ae207e0c b002bc08 46c04718 b510b40c ab04b082 
06-19 10:04:56.734: I/DEBUG(30): ae207e1c 9301cb04 34986804 47a06824 bc10b002 
06-19 10:04:56.734: I/DEBUG(30): code around lr:
06-19 10:04:56.744: I/DEBUG(30): ae20acb4 91099008 f7fb6aa0 900aeab6 1c3a910b 
06-19 10:04:56.744: I/DEBUG(30): ae20acc4 6b646b23 930c1c28 1c31940d f7fd9b0f 
06-19 10:04:56.744: I/DEBUG(30): ae20acd4 4906f88f 44791c28 f7ff3150 b011fe1d 
06-19 10:04:56.754: I/DEBUG(30): ae20ace4 46c0bdf0 000043cc 00004148 00000786 
06-19 10:04:56.754: I/DEBUG(30): ae20acf4 f7fbb510 bd10ec24 4802b510 f7fb4478 
06-19 10:04:56.754: I/DEBUG(30): stack:
06-19 10:04:56.754: I/DEBUG(30):     4689fc20  b295e9e2  
06-19 10:04:56.754: I/DEBUG(30):     4689fc24  4092dccc  
06-19 10:04:56.764: I/DEBUG(30):     4689fc28  00000009  
06-19 10:04:56.764: I/DEBUG(30):     4689fc2c  00000000  
06-19 10:04:56.764: I/DEBUG(30):     4689fc30  0000ab90  [heap]
06-19 10:04:56.764: I/DEBUG(30):     4689fc34  80048c1b  /system/lib/libdvm.so
06-19 10:04:56.764: I/DEBUG(30):     4689fc38  0000ab90  [heap]
06-19 10:04:56.764: I/DEBUG(30):     4689fc3c  4689fc6c  
06-19 10:04:56.774: I/DEBUG(30):     4689fc40  00010004  [heap]
06-19 10:04:56.774: I/DEBUG(30):     4689fc44  80037667  /system/lib/libdvm.so
06-19 10:04:56.774: I/DEBUG(30):     4689fc48  00000000  
06-19 10:04:56.774: I/DEBUG(30):     4689fc4c  afd0dcc4  /system/lib/libc.so
06-19 10:04:56.774: I/DEBUG(30):     4689fc50  afb18a0c  /system/lib/libm.so
06-19 10:04:56.774: I/DEBUG(30):     4689fc54  4689fe00  
06-19 10:04:56.774: I/DEBUG(30):     4689fc58  df002777  
06-19 10:04:56.774: I/DEBUG(30):     4689fc5c  e3a070ad  
06-19 10:04:56.774: I/DEBUG(30): #00 4689fc60  00000045  
06-19 10:04:56.785: I/DEBUG(30):     4689fc64  ad331275  /system/lib/libandroid_runtime.so
06-19 10:04:56.785: I/DEBUG(30):     4689fc68  00000138  
06-19 10:04:56.785: I/DEBUG(30):     4689fc6c  ae20acd7  /system/lib/libandroid_servers.so
06-19 10:04:56.785: I/DEBUG(30):     4689fc70  41adff0c  /dev/ashmem/dalvik-LinearAlloc (deleted)
06-19 10:04:56.785: I/DEBUG(30):     4689fc74  00000001  
06-19 10:04:56.785: I/DEBUG(30): #01 4689fc78  6bea7b7c  
06-19 10:04:56.785: I/DEBUG(30):     4689fc7c  40283d70  /dev/ashmem/dalvik-heap (deleted)
06-19 10:04:56.794: I/DEBUG(30):     4689fc80  6bea7b7c  
06-19 10:04:56.794: I/DEBUG(30):     4689fc84  40283d70  /dev/ashmem/dalvik-heap (deleted)
06-19 10:04:56.794: I/DEBUG(30):     4689fc88  00000000  
06-19 10:04:56.794: I/DEBUG(30):     4689fc8c  00000000  
06-19 10:04:56.794: I/DEBUG(30):     4689fc90  00000000  
06-19 10:04:56.794: I/DEBUG(30):     4689fc94  00000000  
06-19 10:04:56.794: I/DEBUG(30):     4689fc98  00000000  
06-19 10:04:56.794: I/DEBUG(30):     4689fc9c  00000000  
06-19 10:04:56.794: I/DEBUG(30):     4689fca0  00000000  
06-19 10:04:56.794: I/DEBUG(30):     4689fca4  00000000  
06-19 10:04:56.804: I/DEBUG(30):     4689fca8  00c05100  
06-19 10:04:56.804: I/DEBUG(30):     4689fcac  00000138  
06-19 10:04:56.804: I/DEBUG(30):     4689fcb0  4689fe56  
06-19 10:04:56.804: I/DEBUG(30):     4689fcb4  00000001  
06-19 10:04:56.804: I/DEBUG(30):     4689fcb8  0000000a  
06-19 10:04:56.804: I/DEBUG(30):     4689fcbc  4689fde8  
06-19 10:04:56.804: I/DEBUG(30):     4689fcc0  00000000  
06-19 10:04:56.804: I/DEBUG(30):     4689fcc4  807018d5  /system/lib/hw/gps.goldfish.so
06-19 10:04:56.804: I/DEBUG(30):     4689fcc8  00000045  
06-19 10:04:56.804: I/DEBUG(30):     4689fccc  807012cd  /system/lib/hw/gps.goldfish.so
06-19 10:05:10.654: D/Zygote(32): Process 60 terminated by signal (11)
06-19 10:05:10.654: I/Zygote(32): Exit zygote because system server (60) has terminated
06-19 10:05:10.674: I/ActivityThread(233): Removing dead content provider: settings
06-19 10:05:10.674: I/ActivityThread(169): Removing dead content provider: settings
06-19 10:05:10.684: I/ServiceManager(27): service 'SurfaceFlinger' died
06-19 10:05:10.684: I/ServiceManager(27): service 'batteryinfo' died
06-19 10:05:10.684: I/ServiceManager(27): service 'usagestats' died
06-19 10:05:10.684: I/ServiceManager(27): service 'sensorservice' died
06-19 10:05:10.684: I/ServiceManager(27): service 'entropy' died
06-19 10:05:10.684: I/ServiceManager(27): service 'power' died
06-19 10:05:10.684: I/ServiceManager(27): service 'telephony.registry' died
06-19 10:05:10.684: I/ServiceManager(27): service 'account' died
06-19 10:05:10.684: I/ServiceManager(27): service 'content' died
06-19 10:05:10.684: I/ServiceManager(27): service 'package' died
06-19 10:05:10.684: I/ServiceManager(27): service 'activity' died
06-19 10:05:10.684: I/ServiceManager(27): service 'meminfo' died
06-19 10:05:10.684: I/ServiceManager(27): service 'cpuinfo' died
06-19 10:05:10.684: I/ServiceManager(27): service 'permission' died
06-19 10:05:10.684: I/ServiceManager(27): service 'hardware' died
06-19 10:05:10.684: I/ServiceManager(27): service 'battery' died
06-19 10:05:10.684: I/ServiceManager(27): service 'vibrator' died
06-19 10:05:10.684: I/ServiceManager(27): service 'alarm' died
06-19 10:05:10.684: I/ServiceManager(27): service 'window' died
06-19 10:05:10.684: I/ServiceManager(27): service 'statusbar' died
06-19 10:05:10.684: I/ServiceManager(27): service 'clipboard' died
06-19 10:05:10.684: I/ServiceManager(27): service 'device_policy' died
06-19 10:05:10.684: I/ServiceManager(27): service 'network_management' died
06-19 10:05:10.684: I/ServiceManager(27): service 'input_method' died
06-19 10:05:10.684: I/ServiceManager(27): service 'netstat' died
06-19 10:05:10.684: I/ServiceManager(27): service 'wifi' died
06-19 10:05:10.684: I/ServiceManager(27): service 'connectivity' died
06-19 10:05:10.684: I/ServiceManager(27): service 'throttle' died
06-19 10:05:10.684: I/ServiceManager(27): service 'accessibility' died
06-19 10:05:10.694: I/ServiceManager(27): service 'mount' died
06-19 10:05:10.694: I/ActivityThread(364): Removing dead content provider: settings
06-19 10:05:10.694: I/ActivityThread(131): Removing dead content provider: settings
06-19 10:05:10.704: I/ActivityThread(121): Removing dead content provider: settings
06-19 10:05:10.704: I/ActivityThread(126): Removing dead content provider: settings
06-19 10:05:10.724: I/ServiceManager(27): service 'notification' died
06-19 10:05:10.724: I/ServiceManager(27): service 'devicestoragemonitor' died
06-19 10:05:10.724: I/ServiceManager(27): service 'search' died
06-19 10:05:10.724: I/ServiceManager(27): service 'dropbox' died
06-19 10:05:10.724: I/ServiceManager(27): service 'wallpaper' died
06-19 10:05:10.724: I/ServiceManager(27): service 'location' died
06-19 10:05:10.724: I/ServiceManager(27): service 'uimode' died
06-19 10:05:10.724: I/ServiceManager(27): service 'audio' died
06-19 10:05:10.724: I/ServiceManager(27): service 'backup' died
06-19 10:05:10.724: I/ServiceManager(27): service 'appwidget' died
06-19 10:05:10.724: I/ServiceManager(27): service 'diskstats' died
06-19 10:05:10.734: E/installd(34): eof
06-19 10:05:10.734: E/installd(34): failed to read size
06-19 10:05:10.734: I/installd(34): closing connection
06-19 10:05:10.734: D/qemud(37): fdhandler_event: disconnect on fd 11
06-19 10:05:10.754: E/InputQueue-JNI(131): channel '4066e8c8 StatusBar (client)' ~ Publisher closed input channel or an error occurred.  events=0x8
06-19 10:05:10.754: E/InputQueue-JNI(131): channel '4063ba90 TrackingView (client)' ~ Publisher closed input channel or an error occurred.  events=0x8
06-19 10:05:10.834: I/ServiceManager(27): service 'isms' died
06-19 10:05:10.834: I/ServiceManager(27): service 'simphonebook' died
06-19 10:05:10.834: I/ServiceManager(27): service 'iphonesubinfo' died
06-19 10:05:10.834: I/ServiceManager(27): service 'phone' died
06-19 10:05:10.855: I/ServiceManager(27): service 'media.audio_flinger' died
06-19 10:05:10.855: I/ServiceManager(27): service 'media.audio_policy' died
06-19 10:05:10.855: I/ServiceManager(27): service 'media.player' died
06-19 10:05:10.855: I/ServiceManager(27): service 'media.camera' died
06-19 10:05:11.044: I/Netd(376): Netd 1.0 starting
06-19 10:05:11.894: D/AndroidRuntime(377): >>>>>> AndroidRuntime START com.android.internal.os.ZygoteInit <<<<<<
06-19 10:05:11.894: D/AndroidRuntime(377): CheckJNI is ON
06-19 10:05:12.274: I/(375): ServiceManager: 0xad50
06-19 10:05:12.274: D/AudioHardwareInterface(375): setMode(NORMAL)
06-19 10:05:12.285: I/CameraService(375): CameraService started (pid=375)
06-19 10:05:12.324: I/AudioFlinger(375): AudioFlinger's thread 0xc658 ready to run
06-19 10:05:12.964: I/SamplingProfilerIntegration(377): Profiler is disabled.
06-19 10:05:13.024: I/Zygote(377): Preloading classes...
4

1 回答 1

0

我认为您没有将以下权限添加到您的Manifest.xml

<manifest ... >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    ...
</manifest>

您需要它才能访问用户位置。

于 2012-06-19T07:12:32.700 回答