我遇到了一个奇怪的问题,我在服务中配置了一个位置监听器(Google Play服务融合位置)。我使用挂起意图方法(建议用于服务)。
问题是,在使用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" />
如果有任何需要详细了解的地方,请随时向我提问。 非常感谢。