1

我有一项服务,它在 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);
}

}

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

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

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

4

1 回答 1

0

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

在后台获取相同位置更新的原因是,一旦手机处于非活动状态超过 15 分钟,HTC 将禁用添加活动互联网连接(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已报告此问题

于 2013-03-24T08:52:25.967 回答