onLocationChanged没有被姜饼调用

时间:2012-02-29 22:39:01

标签: android gps locationmanager locationlistener

我有一个谜!我可以在下面的代码中使用我的三星Galaxy测试手机,它运行2.2(Froyo)。它不适用于运行2.3.4(Gingerbread)的HTC Thunderbolt测试手机。这与收集位置信息时一样简单,但我也实现了here解释的最佳实践,结果相同:Froyo已经去了,姜饼已经死了。

为了解决这个问题,我应该尝试一些什么?这是我的代码吗?是硬件,软件吗?权限?我激怒了计算机科学之神吗?我会接受给出的第一个答案,并提出一个可以解决问题的建议。谢谢你的帮助!

我的活动:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class SimpleLocationProjectActivity extends Activity {

    private LocationManager mLocationManager;
    private MyLocationListener mLocationListener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationListener = new MyLocationListener();

    }

    @Override
    protected void onPause() {
        mLocationManager.removeUpdates(mLocationListener);
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("SIMPLE APP", "Starting location services.");
        List<String> providers = mLocationManager.getAllProviders();
        for (int i = 0 ; i < providers.size(); i++){
            mLocationManager.requestLocationUpdates(providers.get(i), 1, 1, mLocationListener);
        }

    }

    public class MyLocationListener implements LocationListener{

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            String statusStr;
            switch (status){
            case LocationProvider.OUT_OF_SERVICE:
                statusStr = "Out of Service";
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                statusStr = "Temporarily unavailable";
                break;
            case LocationProvider.AVAILABLE:
                statusStr = "Available";
                break;
            default:
                statusStr = "Huhhh??";  
            }
            Log.i("SIMPLE APP", "Provider:  " + provider + "   Status is:  " + statusStr);


        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.i("SIMPLE APP", "Provider:  " + provider + " is enabled.");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.i("SIMPLE APP", "Provider:  " + provider + " is disabled.");
        }

        @Override
        public void onLocationChanged(Location location) {
            double latitute = location.getLatitude();
            double longitude = location.getLongitude();
            Toast.makeText(SimpleLocationProjectActivity.this, "Lat is " + latitute + "  and lng is " + longitude, Toast.LENGTH_SHORT).show();

            //This never gets called on my HTC Thunderbolt...
            Log.i("SIMPLE APP", "Provider:  " + location.getProvider() + "   Lat is " + latitute + "  and lng is " + longitude);
        }
    }
}

我的宣言:

<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
    package="com.silithcrowe.simplelocationproject" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-sdk android:minSdkVersion="8"/>
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name=".SimpleLocationProjectActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

编辑:logcat运行时的示例:

03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [96]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 2 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.539: D/RPC(1662): read RPC packet
03-01 11:37:43.539: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.539: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.539: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.539: D/RPC(1662): read RPC packet
03-01 11:37:43.539: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.539: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.539: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.539: D/RPC(1662): read RPC packet
03-01 11:37:43.539: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.539: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.539: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.559: D/lib_locapi(1662): loc_eng_report_sv: valid_mask = 0x30, num of sv = 0
03-01 11:37:43.559: D/GpsLocationProvider(1662): reportSvStatus
03-01 11:37:44.410: D/RPC(1662): read RPC packet
03-01 11:37:44.410: D/RPC(1662): read RPC packet size: [208]
03-01 11:37:44.410: V/locapi_rpc_glue(1662): Callback received: 1 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.410: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:44.410: D/lib_locapi(1662): loc_eng_report_position: valid mask = 0x61ed, sess status = 1
03-01 11:37:44.520: D/RPC(1662): read RPC packet
03-01 11:37:44.520: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:44.520: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.520: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:44.520: D/RPC(1662): read RPC packet
03-01 11:37:44.520: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:44.520: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.520: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:44.520: D/RPC(1662): read RPC packet
03-01 11:37:44.520: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:44.520: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.520: D/RPC(1662): written RPC packet size: [28]

请求位置更新时的logcat示例:

I/SIMPLE APP(12524): Starting location services.
D/AK8975  ( 1544): Ignore M_Sensor: (y, p, r) = ( 293,    1,    2), Status = 2
D/GpsLocationProvider( 1662): [handleMessage] message :8
D/GpsLocationProvider( 1662): [handleMessage] ADD_LISTENER
D/GpsLocationProvider( 1662): addListener(com.seedlabs.simplelocationproject)
D/GpsLocationProvider( 1662): setMinTime 0
V/GpsLocationProvider( 1662): enableLocationTracking ++
D/GpsLocationProvider( 1662): [handleMessage] message :3
D/GpsLocationProvider( 1662): [handleMessage] ENABLE_TRACKING
D/GpsLocationProvider( 1662): startNavigating
D/GpsLocationProvider( 1662): HtcBuildFlag.HtcCIQFlag(FALSE)
V/LocationManagerService( 1662): requestLocationUpdates
V/LocationManagerService( 1662): requestLocationUpdatesLocked: updateProvidersLocked
D/LocationManagerService( 1662): [updateProvidersLocked]
V/LocationManagerService( 1662): requestLocationUpdates
V/LocationManagerService( 1662): requestLocationUpdatesLocked: updateProvidersLocked
D/LocationManagerService( 1662): [updateProvidersLocked]
V/LocationManagerService( 1662): requestLocationUpdates
V/LocationManagerService( 1662): requestLocationUpdatesLocked: updateProvidersLocked
D/LocationManagerService( 1662): [updateProvidersLocked]
D/lib_locapi( 1662): [GPS:] loc_eng_set_qos_time_out(standalone = 16000, agps = 16000)
V/GpsLocationProvider( 1662): set_agps_qos_time_out complete
D/GpsLocationProvider( 1662): GpsLocationProvider_set_position_mode
D/lib_locapi( 1662): [GPS:] loc_eng_set_position mode, client = 2, interval = 1000, mode = 0
D/lib_locapi( 1662): [GPS:] loc_eng_set_position mode: GPS_POSITION_MODE_STANDALONE
D/lib_locapi( 1662): [GPS:] loc_eng_set_position mode before IOCTL, interval=1000, mode =4 preferred_accuracy=50, qos_timeout_standalone=16000, recurrence_type=1
V/lib_locapi( 1662): loc_eng_ioctl called: client = 2, ioctl_type = 2
V/locapi_rpc_glue( 1662): loc_ioctl
D/RPC     ( 1662): written RPC packet size: [96]

3 个答案:

答案 0 :(得分:0)

尝试在onCreate()函数

中重复使用此代码
List<String> providers = mLocationManager.getAllProviders();
        for (int i = 0 ; i < providers.size(); i++){
            mLocationManager.requestLocationUpdates(providers.get(i), 1, 1, mLocationListener);
        }

答案 1 :(得分:0)

您是在走动时还是坐在您的开机器上进行测试时使用USB连接以进行logcat输出?我怀疑2.3.4对第二和第三个参数更为严格:

requestLocationUpdates(providers.get(i), 1, 1, mLocationListener)

这些参数限制了硬件生成更新的速率。您目前正在使用的内容仅表示如果位置变化超过一米而且不会超过每秒,则只会给我更新。

来自Location Manager docs

  

如果minDistance大于0,则只会广播一个位置   如果设备移动minDistance米。获取通知为   尽可能经常将两个参数都设置为0。

试试这个是否有所作为。如果没有,请发布您的logcat输出以获取更多想法。

requestLocationUpdates(providers.get(i), 0, 0, mLocationListener)

答案 2 :(得分:0)

我在Android平板电脑上遇到了同样的问题,直到我意识到我没有启用Google位置服务。一旦我这样做,onLocationChanged就开始根据我连接的WiFi网络给我更新。有关如何启用它,请参阅此help document(设置 - &gt;大多数Android设备上的位置)。