相同的位置在后台更新,即使我已经移动显着

时间:2013-03-21 06:12:57

标签: android geolocation android-service android-location

我有一个服务,它在onCreateMethod中请求网络位置更新。我已使用LocationListener接收新位置,提供程序的状态以及提供程序中的statusChange。

当设备处于低功耗状态(手机屏幕关闭)时,我会获得与手机屏幕处于活动状态(屏幕开启)时网络提供商相同的位置修复。

public class LocationRegisterService extends Service implements LocationListener{

private String TAG = LocationRegisterService.class.getName();

private LocationManager lm;

public void onCreate()
{
    Log.d(TAG,"Entering LocationRegisterService::onCreate()");
    //Register for network location updates
    lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    Log.i(TAG,"Regsitering for Network Location Updates");
    Log.i(TAG,"minTime = 0,minDistance = 0");
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
            Log.d(TAG,"Exiting LocationRegisterSerivce::onCreate()");
}

public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.d(TAG,"Entering LocationRegisterService::onStartCommand()");
    Log.d(TAG,"Exiting LocationRegisterService::onStartCommand()");
    return Service.START_STICKY;
}


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onDestroy() 
{
    super.onDestroy();
    Log.d(TAG,"Entering LocationRegisterService::onDestroy()");
    lm.removeUpdates(this);
    Log.d(TAG,"Exiting LocationRegisterService::onDestroy()");
}

@Override
public void onLocationChanged(Location location) {
    Helpers.logScreenStatus(this);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    double accuracy = location.getAccuracy();
    String location_timestamp = formatTime(location.getTime());
    String log = latitude + "," + longitude +","+accuracy+","+location_timestamp;
    Log.i(TAG,log);
}

@Override
public void onProviderDisabled(String provider) {
    String log = "Network Provider Disabled";
}

@Override
public void onProviderEnabled(String provider) {
    String log = "Network Provider Enabled";
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    String log = "";
    switch(status){
        case LocationProvider.AVAILABLE:
            log = "Network Available";
            break;
        case LocationProvider.OUT_OF_SERVICE:
            log = "Network Out Of Service";
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            log = "Network Temporarily Unavailable";
            break;
    }
    setServiceWakeUpTime();
    stopSelf();
}

private void setServiceWakeUpTime()
{
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this,AlarmReceiver.class);
    PendingIntent operation = PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    long wakeAt = System.currentTimeMillis() + (60 * 1000);
    am.set(AlarmManager.RTC_WAKEUP, wakeAt , operation);
    FileLogger.log(this, "Service will wake up after one minute");
}

private String formatTime(long epoch)
{
    Date date = new Date(epoch);
    String pattern = "y/M/d H:m:s.S";
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    return dateFormat.format(date);
}

}

我从一项活动开始这项服务,并在设备屏幕关闭的情况下走了近50KM。我附上了旅行的日志。

2013/3/21 0:29:58.743  Entering Broadcast Receiver
2013/3/21 0:29:58.756  Screen is off
2013/3/21 0:29:58.778  Exiting Broadcast Receiver
2013/3/21 0:29:58.783  Registering for network location updates
2013/3/21 0:30:53.571  Screen is off
2013/3/21 0:30:53.587  13.07383585,80.22151335000001,50.0,2013/3/21 0:30:8.551
2013/3/21 0:30:53.593  Screen is off
2013/3/21 0:30:53.595  Network Temporarily Unavailable
2013/3/21 0:30:53.613  Service will wake up after one minute
2013/3/21 0:30:53.619  UnRegistering listener for location updates
2013/3/21 0:31:53.676  Entering Broadcast Receiver
2013/3/21 0:31:53.689  Screen is off
2013/3/21 0:31:53.710  Exiting Broadcast Receiver
2013/3/21 0:31:58.516  Registering for network location updates
2013/3/21 0:32:17.583  Screen is off
2013/3/21 0:32:17.594  13.07383585,80.22151335000001,50.0,2013/3/21 0:32:3.544

相同的日志重复了将近一个小时。此代码在Android 2.3.3的HTC Explorer中运行。有没有人能回答这种行为?

1 个答案:

答案 0 :(得分:0)

获得相同位置更新的问题仅在HTC手机中发生。我不知道这是否会出现在所有HTC Android手机上。但它确实发生在我的HTC Explorer上。

在后台获得相同位置更新的原因是,一旦手机处于非活动状态超过15分钟,HTC将禁用添加活动的Internet连接(WIFI或移动数据连接)。为了交叉检查,我写了下面的代码。

  • 安排在一分钟后调用AlarmReceiver的活动。
  • 警报接收器检查是否存在可以建立互联网连接的任何连接。
  • 警报接收器在一分钟后注册警报以自行呼叫

我让代码彻夜不眠。

public class AlarmReceiver extends BroadcastReceiver {

private static String TAG = AlarmReceiver.class.getName();

@Override
public void onReceive(Context context, Intent intent) 
{
    Log.i(TAG, "Entering Broadcast Receiver onReceive");
    FileLogger.log(context, "Entering Broadcast Receiver");
    logActiveNetworkInfo(context);
    setAlarm(context);
    FileLogger.log(context, "Exiting Broadcast Receiver");
    Log.i(TAG, "Exiting Broadcast Receiver onReceive");
}

private void logActiveNetworkInfo(Context context)
{
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if(networkInfo == null)
    {
        FileLogger.log(context, "No network connectivity");
        return;
    }
    String networkTypeName = networkInfo.getTypeName();
    boolean isConnected = networkInfo.isConnected();
    String log = "";
    if(isConnected == true)
    {
        log = "Internet available through " + networkTypeName;
    }
    else
        log = "Internet not available through "+ networkTypeName;
    FileLogger.log(context, log);
    Log.i(TAG,log);
    return;
}

private void setAlarm(Context context)
{
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long wakeUpAt = System.currentTimeMillis() + (60 * 1000);
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent operation = PendingIntent.getBroadcast(context,
                                                         0,
                                                         intent,
                                                         PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, wakeUpAt , operation);
    Log.i(TAG,"Alarm set to ring after "+wakeUpAt + " milliseconds");
}

}

因此,应用程序开发人员要小心,如果您想在HTC手机的后台进行HTTP呼叫,如果手机处于非活动状态超过15分钟,则会失败。

此问题已报告HTC Deep Sleep