Gps 使用LocationListener(使用google play服务)作为服务会消耗太多电池(Android 4.4.2)

Gps 使用LocationListener(使用google play服务)作为服务会消耗太多电池(Android 4.4.2),gps,android-location,Gps,Android Location,我有一个每30秒返回当前位置的服务,我的问题是该服务消耗了大约40%的电池。GPS图标始终处于活动状态,即使我增加了时间间隔。我发现我的Nexus 4 android 4.4.2存在这个问题。一旦调用callback OnLocationChanged,GPS将一直处于唤醒状态,这将消耗整个电池。对于我的另一部手机NexusOne android 2.2.3,我看不出这个问题,GPS每30秒打开一次,然后关闭一次。我没有电池组合。服务使用位置请求,具有.setPriority(LocationR

我有一个每30秒返回当前位置的服务,我的问题是该服务消耗了大约40%的电池。GPS图标始终处于活动状态,即使我增加了时间间隔。我发现我的Nexus 4 android 4.4.2存在这个问题。一旦调用callback OnLocationChanged,GPS将一直处于唤醒状态,这将消耗整个电池。对于我的另一部手机NexusOne android 2.2.3,我看不出这个问题,GPS每30秒打开一次,然后关闭一次。我没有电池组合。服务使用位置请求,具有.setPriority(LocationRequest.PRIORITY\u HIGH\u精度),但使用GPS和网络除外,情况并非如此。我认为安卓4.4.2有问题,或者谷歌play服务有问题

这里是我的服务代码:

public class MyServiceGpsDebug extends Service implements       
 GooglePlayServicesClient.ConnectionCallbacks,    
 GooglePlayServicesClient.OnConnectionFailedListener,LocationListener 
 {

     IBinder mBinder = new LocalBinder();

      private LocationClient mLocationClient;
      private LocationRequest mLocationRequest;
      // Flag that indicates if a request is underway.
      private boolean mInProgress;

      private Boolean servicesAvailable = false;

      public class LocalBinder extends Binder 
      {
            public MyServiceGpsDebug getServerInstance() 
            {
                return MyServiceGpsDebug.this;
            }
      }

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

          mInProgress = false;
          // Create the LocationRequest object
          mLocationRequest = LocationRequest.create();
          // Use high accuracy
          mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
          // Set the update interval to 5 seconds
          mLocationRequest.setInterval(Constants.UPDATE_INTERVAL);
          // Set the fastest update interval to 1 second
          mLocationRequest.setFastestInterval(Constants.FASTEST_INTERVAL);

          servicesAvailable = servicesConnected();
          mLocationClient = new LocationClient(this, this, this);   
      }

          private boolean servicesConnected() 
      {

          // Check that Google Play services is available
          int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

          // If Google Play services is available
          if (ConnectionResult.SUCCESS == resultCode) 
          {
              return true;
          } 
          else
          {
              return false;
          }
      }

      public int onStartCommand (Intent intent, int flags, int startId)
      {
          super.onStartCommand(intent, flags, startId);

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

          setUpLocationClientIfNeeded();
          if(!mLocationClient.isConnected() || !mLocationClient.isConnecting() && !mInProgress)
          {
            appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Started", Constants.LOG_FILE);
            mInProgress = true;
            mLocationClient.connect();
          }

          return START_STICKY;
      }

      private void setUpLocationClientIfNeeded()
      {
            if(mLocationClient == null) 
                  mLocationClient = new LocationClient(this, this, this);
      }

          @Override
      public void onLocationChanged(Location location) 
      {
          // Report to the UI that the location was updated
          String msg = Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude());
          Log.d("debug", msg);
          // Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
          appendLog(msg, Constants.LOCATION_FILE);
      }

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

      public String getTime() 
      {
            SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return mDateFormat.format(new Date());
      }

      public void appendLog(String text, String filename)
      {       
             File logFile = new File(filename);
             if (!logFile.exists())
             {
                try
                {
                   logFile.createNewFile();
                } 
                catch (IOException e)
                {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                }
             }
             try
             {
                //BufferedWriter for performance, true to set append to file flag
                BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
                buf.append(text);
                buf.newLine();
                buf.close();
             }
             catch (IOException e)
             {
                // TODO Auto-generated catch block
                e.printStackTrace();
             }
      }

      @Override
      public void onDestroy()
      {
          // Turn off the request flag
          mInProgress = false;
          if(servicesAvailable && mLocationClient != null)
          {
                mLocationClient.removeLocationUpdates(this);
                // Destroy the current location client
                mLocationClient = null;
          }
          // Display the connection status
          // Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
          appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Stopped", Constants.LOG_FILE);
          super.onDestroy();  
      }


      @Override
      public void onConnected(Bundle bundle) 
      {

          // Request location updates using static settings
          mLocationClient.requestLocationUpdates(mLocationRequest, this);
          appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Connected", Constants.LOG_FILE);

      }

      @Override
      public void onDisconnected() 
      {
          // Turn off the request flag
          mInProgress = false;
          // Destroy the current location client
          mLocationClient = null;
          // Display the connection status
          // Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
          appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected", Constants.LOG_FILE);
      }


      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) 
      {
        mInProgress = false;

          if (connectionResult.hasResolution()) 
          {

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

          }
      }


      public final class Constants 
      {

            // Milliseconds per second
            private static final int MILLISECONDS_PER_SECOND = 1000;
            // Update frequency in seconds
            private static final int UPDATE_INTERVAL_IN_SECONDS = 60;
            // Update frequency in milliseconds
            public static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;
            // The fastest update frequency, in seconds
            private static final int FASTEST_INTERVAL_IN_SECONDS = 60;
            // A fast frequency ceiling in milliseconds
            public static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;
            // Stores the lat / long pairs in a text file
            public static final String LOCATION_FILE = "sdcard/location.txt";
            // Stores the connect / disconnect data in a text file
            public static final String LOG_FILE = "sdcard/log.txt";


            /**
             * Suppress default constructor for noninstantiability
             */
            private Constants() {
                throw new AssertionError();
            }
      }
}

我对Nexus4也有类似的问题。我的应用有一个使用位置更新的服务(fusion location或android提供商,而不是两者)。所有android手机都可以正常工作,但在Nexus4中,一段时间后,即使我关闭了应用程序,手机也会变热变慢(通过DDMS,100%停止)。唯一的解决办法是杀掉Google play服务。
我认为Nexus4的服务中有一个bug。每隔30分钟就有一个肮脏的解决方案可以杀死Google Play服务,例如,如果phone=nexus4,但我不知道这是否可能

最后是一个和我有同样问题的人:)。关于这个bug已经有两个问题了:和这个问题,但是没有答案,正在等待修复。是的,对不起,我想添加一个评论(不是答案)。。。我的错:-)@benomma777,升级到Google Play Services 4.4.52后,问题似乎得到了解决。我的N4没有准备好自动更新,所以从XDA()获得了apk(对于Nexus4,您需要-036版本)。刚刚做了一个小时的压力测试(3G,gps更新和你的值相似),一切都很好。尚未更新应用程序的播放服务库,因为它尚未在android SDK中提供manager@benomma777,我认为在你的情况下,全球定位系统将保持不变。在姜饼的情况下,事情的工作方式不同,这可以在使用GPS时的系统日志中看到。也许您必须停止onLocationChanged中的位置,并使用一些计时器重新启动。另外,请设置mLocationRequest.setSmallestDisplacement(5)//例如5米以节省大量电池。这意味着,如果用户未移动5米,GPS可能不会获取新值。