位置监听器在调试模式下工作,但在编译的APK中没有

时间:2014-01-08 13:55:14

标签: android eclipse geolocation

我有一个奇怪的问题,我在服务中配置了一个位置监听器(Google Play服务融合位置)。我使用挂起的intent方法(推荐用于服务)。

问题是,当我使用Eclipse进行调试时,一切正常,我会定期收到位置更新。但是当我编译apk并运行它时,行为会发生变化,位置更新只会被调用一次。

以下是简化代码:

public class TrackingService extends Service implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

        //fields

    private PendingIntent locPendingIntent;

    // ====== CONFIGURATION ========

    BroadcastReceiver locationReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Do this when the system sends the intent
            if (intent.getAction().equals(AppManager.ACTION_LOCATION_READY)) {

                // Called only once on compiled apk. Working fine when debugging


                Bundle b = intent.getExtras();
                Location loc = (Location) b
                        .get(LocationClient.KEY_LOCATION_CHANGED);


                if(loc != null) {

                    onLocationChanged(loc); 
                }
            }

        }
    };

    /*
     * Create a new location client, using the enclosing class to handle
     * callbacks.
     */
    private void setUpLocationClientIfNeeded() {
        if (mLocationClient == null)
            mLocationClient = new LocationClient(this, this, this);
    }

    // ======== LIFE CYCLE ========

    @Override
    public void onCreate() {
        super.onCreate();

        mInProgress = false;
        // Create the LocationRequest object
        mLocationRequest = LocationRequest.create();
        mLocationRequest
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        // Set the update interval to 5 seconds
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        // Set the fastest update interval to 2.5 second
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);


        setUpLocationClientIfNeeded();

        registerReceiver(locationReceiver, new IntentFilter(AppManager.ACTION_LOCATION_READY));
    }

    public int onStartCommand(Intent intent, int flags, int startId) {

        super.onStartCommand(intent, flags, startId);

        if (mLocationClient.isConnected() || mInProgress)
            return START_STICKY;

        setUpLocationClientIfNeeded();

        if (!mLocationClient.isConnected() || !mLocationClient.isConnecting()
                && !mInProgress) {
            mInProgress = true;
            mLocationClient.connect();
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    // ===== GOOGLE PLAY SERVICES CALLBACKS ======

    /*
     * Called by Location Services when the request to connect the client
     * finishes successfully. At this point, you can request the current
     * location or start periodic updates
     */
    public void onConnected(Bundle bundle) {

        startListeningForLocations();

    }

    /*
     * Called by Location Services if the connection to the location client
     * drops because of an error.
     */
    public void onDisconnected() {
        // Turn off the request flag
        mInProgress = false;
        // Destroy the current location client
        mLocationClient = null;

    }

    /*
     * Called by Location Services if the attempt to Location Services fails.
     */
    public void onConnectionFailed(ConnectionResult connectionResult) {
        mInProgress = false;
        /*
         * Google Play services can resolve some errors it detects. If the error
         * has a resolution, try sending an Intent to start a Google Play
         * services activity that can resolve error.
         */
        if (connectionResult.hasResolution()) {

        } else {
            // If no resolution is available, display an error dialog

        }
    }


    private void startListeningForLocations() {

        Intent intent = new Intent(AppManager.ACTION_LOCATION_READY);
        locPendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        mLocationClient.requestLocationUpdates(mLocationRequest,
                locPendingIntent);

    }

    // Define the callback method that receives location updates
    public void onLocationChanged(Location location) {

        // process with location

    }



}

清单权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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_MOCK_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

如果有人需要更多细节,请问我。 非常感谢你。

1 个答案:

答案 0 :(得分:0)

我有同样的问题,但我想我可以解决它。 在我的代码中,我有一个Activity和一个Service。我从服务中调用了位置客户端。在调试模式下,它工作正常,但是当我对发布APK进行修改时,它无法正常工作。

我创建了一个调用位置客户端的方法,而不是从服务调用,我从活动中调用它(在onServiceConnected方法的末尾)并且它工作正常。

我希望它适合你。

祝你好运, 莱昂纳多。