Java 在服务中获取位置

Java 在服务中获取位置,java,android,gps,geolocation,location,Java,Android,Gps,Geolocation,Location,编辑:我在最近一次尝试的底部添加了代码 我试图遵循示例代码。我很难找到最后一个已知的位置。我试过几种方法 在上面的链接示例中,它们使用“GPSTracker”。它给出错误“无法解析符号‘GPSTracker’”。我有没有遗漏什么重要的东西 我尝试使用LocationManager获取位置,如中所示,但出现错误“无法解析方法'requestLocationUpdates(java.lang.String,long,long,匿名java.util.TimerTask)'”。我还尝试将第三个参数更

编辑:我在最近一次尝试的底部添加了代码


我试图遵循示例代码。我很难找到最后一个已知的位置。我试过几种方法

  • 在上面的链接示例中,它们使用“GPSTracker”。它给出错误“无法解析符号‘GPSTracker’”。我有没有遗漏什么重要的东西
  • 我尝试使用LocationManager获取位置,如中所示,但出现错误“无法解析方法'requestLocationUpdates(java.lang.String,long,long,匿名java.util.TimerTask)'”。我还尝试将第三个参数更改为float,得到了类似的错误
  • 我试图用FusedLocationProviderApi获取位置。我在onCreate()方法中添加了新的GoogleAppClient.Builder,导入了com.google.android.gms.location.FusedLocationProviderApi,并更新了build.gradle文件。但是当我将“this”添加到.addConnectionCallbacks(this)和.addOnConnectionFailedListener(this)中时,新的GoogleAppClient.Builder方法会出现错误。从我所读到的,这是最好的选择,因为它是优化的最低限度的电池使用
  • 我有点不知所措,不知该如何找到那个位置。我的目标类似于第一个链接示例。我想构建一个在后台运行的服务,并在指定的时间间隔内将其位置的更新发送到服务器。到目前为止,我已经创建了两个按钮,允许服务启动和停止。一旦服务启动,它会检查设备是否有移动数据。然后我想让它检查位置是否打开,是否有GPS信号。我只需要GPS信号,不需要网络位置。最后,如果数据和位置/GPS信号均为真,则需要在指定的时间间隔内将其位置发送给服务器。这是到目前为止我的代码

    MainActivity.java

    package com.testtracker;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    
    public class MainActivity extends Activity
    {
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyService.setUp(this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    public void startService(View view)
    {
        startService(new Intent(getBaseContext(), MyService.class));
    }
    
    public void stopService(View view)
    {
        stopService(new Intent(getBaseContext(), MyService.class));
    }
    }
    
    package com.testtracker;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity implements Button.OnClickListener
    {
    Button startButton;
    Button stopButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        startButton = (Button)findViewById(R.id.button2);
        stopButton = (Button)findViewById(R.id.button);
    
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
    
        LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);
        boolean locationEnabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
        if(!locationEnabled)
        {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    
        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.button2:
                startButton.setEnabled(false);
                stopButton.setEnabled(true);
    
                Intent intentStart = new Intent(this, MyService.class);
                startService(intentStart);
                break;
    
            case R.id.button:
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
    
                Intent intentStop = new Intent(this, MyService.class);
                stopService(intentStop);
                break;
        }
    
    }
    }
    
    MyService.java

    package com.testtracker;
    
    import android.app.Activity;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.os.IBinder;
    import android.os.PowerManager;
    import android.widget.Button;
    import android.widget.Toast;
    
    import java.lang.reflect.Method;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class MyService extends Service
    {
    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;
    static Button startButton;
    static Button stopButton;
    
    public static void setUp(Activity act)
    {
        startButton = (Button)act.findViewById(R.id.button2);
        stopButton = (Button)act.findViewById(R.id.button);
    }
    
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();
    
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
    
        final ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    
        Timer timer = new Timer();
        final int TIME_INTERVAL = 10000;
    
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    
        timer.schedule(new TimerTask()
        {
            boolean mobileDataEnabled = false;
    
            public void run() {
                try {
                    //Check if phone has data
                    Class cmClass = Class.forName(connectivityManager.getClass().getName());
                    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
                    method.setAccessible(true);
                    mobileDataEnabled = (Boolean) method.invoke(connectivityManager);
    
                    if (mobileDataEnabled)
                    {
                        //Has data. Check if phone has location and GPS signal. If it does, get location and send to server.
                    }
                    else
                    {
                        //No data. Check again during the next timer interval.
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, TIME_INTERVAL);
    
        return START_STICKY;
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        wakeLock.release();
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }
    }
    
    package com.testtracker;
    
    import android.app.Activity;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.net.ConnectivityManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.os.Looper;
    import android.os.PowerManager;
    import android.widget.Toast;
    
    import java.lang.reflect.Method;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class MyService extends Service implements LocationListener
    {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    
    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;
    
    public void onCreate()
    {
        Toast.makeText(this, "in on create", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();
    
        final ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    
        Timer timer = new Timer();
        final int TIME_INTERVAL = 10000;
    
        Looper.prepare(); //Fatal error said this must be added?
    
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    
        timer.schedule(new TimerTask()
        {
            boolean mobileDataEnabled = false;
    
            public void run() {
                try {
                    //Check if phone has data
                    Class cmClass = Class.forName(connectivityManager.getClass().getName());
                    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
                    method.setAccessible(true);
                    mobileDataEnabled = (Boolean) method.invoke(connectivityManager);
    
                    if (mobileDataEnabled)
                    {
                        //Has data. Check if phone has location and GPS signal. If it does, get location and send to server.
                        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    
                        final Criteria cri = new Criteria();
    
                        String bestProvider = locationManager.getBestProvider(cri, true);
    
                        Location loc = locationManager.getLastKnownLocation(bestProvider);
                        double lat = loc.getLatitude();
                        double lng = loc.getLongitude();
    
                        Toast.makeText(getBaseContext(), lat + " - " + lng, Toast.LENGTH_SHORT).show();
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                    onDestroy();
                }
            }
        }, 0, TIME_INTERVAL);
    
        return START_STICKY;
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        wakeLock.release();
    
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onLocationChanged(Location location) {
    
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    
    }
    }
    
    编辑: 通过我的尝试(记录在下面的注释中),我想我应该发布我的更新代码。我将用我当前的尝试更新下面的代码,并将上面的代码保留为我的原始代码。理想情况下,我希望使用FusedLocationProviderApi或Little Fluffy Location库,因为它们似乎是为在后台运行而优化的

    MainActivity.java

    package com.testtracker;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    
    public class MainActivity extends Activity
    {
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyService.setUp(this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    public void startService(View view)
    {
        startService(new Intent(getBaseContext(), MyService.class));
    }
    
    public void stopService(View view)
    {
        stopService(new Intent(getBaseContext(), MyService.class));
    }
    }
    
    package com.testtracker;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity implements Button.OnClickListener
    {
    Button startButton;
    Button stopButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        startButton = (Button)findViewById(R.id.button2);
        stopButton = (Button)findViewById(R.id.button);
    
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
    
        LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);
        boolean locationEnabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
        if(!locationEnabled)
        {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    
        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.button2:
                startButton.setEnabled(false);
                stopButton.setEnabled(true);
    
                Intent intentStart = new Intent(this, MyService.class);
                startService(intentStart);
                break;
    
            case R.id.button:
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
    
                Intent intentStop = new Intent(this, MyService.class);
                stopService(intentStop);
                break;
        }
    
    }
    }
    
    MyService.java

    package com.testtracker;
    
    import android.app.Activity;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.os.IBinder;
    import android.os.PowerManager;
    import android.widget.Button;
    import android.widget.Toast;
    
    import java.lang.reflect.Method;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class MyService extends Service
    {
    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;
    static Button startButton;
    static Button stopButton;
    
    public static void setUp(Activity act)
    {
        startButton = (Button)act.findViewById(R.id.button2);
        stopButton = (Button)act.findViewById(R.id.button);
    }
    
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();
    
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
    
        final ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    
        Timer timer = new Timer();
        final int TIME_INTERVAL = 10000;
    
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    
        timer.schedule(new TimerTask()
        {
            boolean mobileDataEnabled = false;
    
            public void run() {
                try {
                    //Check if phone has data
                    Class cmClass = Class.forName(connectivityManager.getClass().getName());
                    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
                    method.setAccessible(true);
                    mobileDataEnabled = (Boolean) method.invoke(connectivityManager);
    
                    if (mobileDataEnabled)
                    {
                        //Has data. Check if phone has location and GPS signal. If it does, get location and send to server.
                    }
                    else
                    {
                        //No data. Check again during the next timer interval.
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, TIME_INTERVAL);
    
        return START_STICKY;
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        wakeLock.release();
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }
    }
    
    package com.testtracker;
    
    import android.app.Activity;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.net.ConnectivityManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.os.Looper;
    import android.os.PowerManager;
    import android.widget.Toast;
    
    import java.lang.reflect.Method;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class MyService extends Service implements LocationListener
    {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    
    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;
    
    public void onCreate()
    {
        Toast.makeText(this, "in on create", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
        wakeLock.acquire();
    
        final ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    
        Timer timer = new Timer();
        final int TIME_INTERVAL = 10000;
    
        Looper.prepare(); //Fatal error said this must be added?
    
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    
        timer.schedule(new TimerTask()
        {
            boolean mobileDataEnabled = false;
    
            public void run() {
                try {
                    //Check if phone has data
                    Class cmClass = Class.forName(connectivityManager.getClass().getName());
                    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
                    method.setAccessible(true);
                    mobileDataEnabled = (Boolean) method.invoke(connectivityManager);
    
                    if (mobileDataEnabled)
                    {
                        //Has data. Check if phone has location and GPS signal. If it does, get location and send to server.
                        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    
                        final Criteria cri = new Criteria();
    
                        String bestProvider = locationManager.getBestProvider(cri, true);
    
                        Location loc = locationManager.getLastKnownLocation(bestProvider);
                        double lat = loc.getLatitude();
                        double lng = loc.getLongitude();
    
                        Toast.makeText(getBaseContext(), lat + " - " + lng, Toast.LENGTH_SHORT).show();
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                    onDestroy();
                }
            }
        }, 0, TIME_INTERVAL);
    
        return START_STICKY;
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        wakeLock.release();
    
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onLocationChanged(Location location) {
    
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    
    }
    }
    
    logcat 错误看起来像是由Looper.prepare()引起的。如果我没有添加,则会出现致命错误“无法在未调用Looper.prepare()的线程内创建处理程序”。这一次你赢不了吗

    2263-2263/com.testtracker E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.testtracker, PID: 2263
    java.lang.RuntimeException: Unable to start service com.testtracker.MyService@18edb4e4 with Intent { cmp=com.testtracker/.MyService }: java.lang.RuntimeException: Only one Looper may be created per thread
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2914)
    at android.app.ActivityThread.access$2100(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
    at android.os.Looper.prepare(Looper.java:76)
    at android.os.Looper.prepare(Looper.java:71)
    at com.testtracker.MyService.onStartCommand(MyService.java:53)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2897)
    at android.app.ActivityThread.access$2100(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    

    我尝试使用它,因为它看起来像是电池性能优化以及。我遵循了,但出现了构建错误。“错误:无法访问GooglePlayServicesClient错误:无法访问OnConnectionFailedListener”我不担心上述两个错误,因为我希望在如上所述的指定时间间隔内获取GPS位置。任何建议都将不胜感激!我正在继续尝试解决这个问题,但还没有成功。我最近的一次尝试是跟随。我似乎取得了一些进展,但这个问题并不完整,OP也从未对解决方案做出回应。使用在那里找到的信息,我现在遇到了致命的异常错误,无法启动服务。任何关于如何在给定间隔内通过上述服务获取位置的建议都将不胜感激!可能重复我尝试使用的,因为它看起来是优化电池性能以及。我遵循了,但出现了构建错误。“错误:无法访问GooglePlayServicesClient错误:无法访问OnConnectionFailedListener”我不担心上述两个错误,因为我希望在如上所述的指定时间间隔内获取GPS位置。任何建议都将不胜感激!我正在继续尝试解决这个问题,但还没有成功。我最近的一次尝试是跟随。我似乎取得了一些进展,但这个问题并不完整,OP也从未对解决方案做出回应。使用在那里找到的信息,我现在遇到了致命的异常错误,无法启动服务。任何关于如何在给定间隔内通过上述服务获取位置的建议都将不胜感激!可能重复的