我正在尝试在屏幕关闭时记录GPS位置。
这是我的清单文件(请注意WAKE_LOCK和GPS权限以及LocationService定义):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joshuajwitter.backgroundgpstest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BackgroundGPSTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.LocationService" >
<intent-filter>
<action android:name="com.joshuajwitter.backgroundgpstest.service.LocationService" />
</intent-filter>
</service>
</application>
</manifest>
这是我的后台服务:
package com.joshuajwitter.backgroundgpstest.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
public class LocationService extends Service {
private static final String TAG = "LocationService";
// the location manager and listener
LocationManager m_locationManager;
GPSLocationListener m_gpsLocationListener;
// the dreaded wakelock
WakeLock m_wakeLock;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "Starting the LocationService");
// acquire the wakelock
Log.d(TAG, "Acquiring the wake lock");
m_wakeLock.acquire();
}
@Override
public void onCreate() {
// get the wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
m_wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"String Wake Lock");
// instantiate the gps listener
m_gpsLocationListener = new GPSLocationListener();
// get the location manager
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// request location updates every 5 seconds
m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 0, m_gpsLocationListener);
}
@Override
public void onDestroy() {
Log.d(TAG, "Stopping the LocationService");
// if the wakelock is held
if (m_wakeLock.isHeld()) {
// release it
Log.d(TAG, "Releasing the wake lock");
m_wakeLock.release();
}
// remove the listener
m_locationManager.removeUpdates(m_gpsLocationListener);
}
private class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Got a new location frome the LocationManager [" + location.getLatitude()
+ ", " + location.getLongitude() + "]");
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
..以下是启动和停止服务的活动:
package com.joshuajwitter.backgroundgpstest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.joshuajwitter.backgroundgpstest.service.LocationService;
public class BackgroundGPSTestActivity extends Activity {
private boolean m_serviceRunning = false;
private Button m_mainButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the main button
m_mainButton = ((Button) findViewById(R.id.mainButton));
// set the main button listener
m_mainButton
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// if the service is not running
if (!m_serviceRunning) {
// start the service
startService();
}
else {
// start the service
stopService();
}
}
});
}
private void startService() {
m_serviceRunning = true;
startService(new Intent(this, LocationService.class));
m_mainButton.setText("Stop Background GPS Service");
}
private void stopService() {
m_serviceRunning = false;
stopService(new Intent(this, LocationService.class));
m_mainButton.setText("Start Background GPS Service");
}
}
当我运行该服务并保持屏幕开启时,我获得了正确的GPS经纬度。然而,当我关闭屏幕时,我得到相同的重复输出:
01-31 15:48:58.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:48:59.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:00.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:01.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:02.477: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:03.477: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:04.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
当我重新打开屏幕时,我再次获得正确的GPS值。这是踢球者:谷歌地图导航在我的手机上运行,屏幕关闭。如果我在测试程序的同时运行它,则会出现相同的行为...当我的测试应用程序反复重复相同的纬度/经度时,导航应用会收到正确坐标的通知。
这是在运行2.3.3 Gingerbread的Android手机上。感谢您的任何意见!