可以停止位置更新,Android服务

可以停止位置更新,Android服务,第1张

概述我正在尝试创建路线跟踪应用.即使应用程序在后台,它也需要跟踪位置.所以我创建了一个服务,并向该服务添加了代码.以下是我的代码.但是有一个问题.我从主要活动开始服务.publicvoidstartTracking(Viewview){startService(newIntent(MainActivity.this,LocationIntentSe

我正在尝试创建路线跟踪应用.即使应用程序在后台,它也需要跟踪位置.所以我创建了一个服务,并向该服务添加了代码.以下是我的代码.但是有一个问题.我从主要活动开始服务.

public voID startTracking(VIEw vIEw) {    startService(new Intent(MainActivity.this, LocationIntentService.class));}public voID stopTracking(VIEw vIEw) {    stopService(new Intent(MainActivity.this, LocationIntentService.class));}

它启动服务,并将位置插入到本地数据库中.但是我不能停止这些服务.当我停止使用上述代码的服务时,它仍会跟踪位置.我如何停止位置更新.

public class LocationIntentService extends IntentService implements LocationListener, Googleapiclient.ConnectionCallbacks, Googleapiclient.OnConnectionFailedListener {    private static final String TAG = LocationIntentService.class.getSimplename();    private static final long INTERVAL = 1000 * 10;    private static final long FASTEST_INTERVAL = 1000 * 5;    private static int disPLACEMENT = 10;    LocationRequest mLocationRequest;    Googleapiclient mGoogleapiclient;    Location mLastLocation;    DBAdapter dbAdapter;    public LocationIntentService() {        super("LocationIntentService");    }    @OverrIDe    protected voID onHandleIntent(Intent intent) {        Log.e(TAG, " ***** Service on handled");        if (isGooglePlayServicesAvailable()) {            createLocationRequest();            mGoogleapiclient = new Googleapiclient.Builder(this)                    .addAPI(LocationServices.API)                    .addConnectionCallbacks(this)                    .addOnConnectionFailedListener(this)                    .build();            mGoogleapiclient.connect();        }    }    @OverrIDe    public voID onConnected(Bundle bundle) {        Log.e(TAG, " ***** Service on connected");        startLocationUpdates();        openDB();    }    @OverrIDe    public voID onConnectionSuspended(int i) {        Log.e(TAG, " ***** Service on suspended");        mGoogleapiclient.connect();    }    @OverrIDe    public voID onLocationChanged(Location location) {        Log.e(TAG, "Location changed");        mLastLocation = location;        String latitude = String.valueOf(mLastLocation.getLatitude());        String longitude = String.valueOf(mLastLocation.getLongitude());        Log.e(TAG, " ##### Got new location"+ latitude+ longitude);        Time today = new Time(Time.getCurrentTimezone());        today.setToNow();        String timestamp = today.format("%Y-%m-%d %H:%M:%s");        dbAdapter.insertRow(latitude, longitude, timestamp);    }    @OverrIDe    public voID onConnectionFailed(ConnectionResult connectionResult) {        Log.e(TAG, "Connection Failed: ConnectionResult.getErrorCode() = "                + connectionResult.getErrorCode());    }    @OverrIDe    public voID onDestroy() {        Log.e(TAG, "Service is Destroying...");        super.onDestroy();        if (mGoogleapiclient.isConnected()) {            stopLocationUpdates();            mGoogleapiclient.disconnect();        }        closeDB();    }    protected voID stopLocationUpdates() {        Log.d(TAG, "Location update stoPing...");        LocationServices.FusedLocationAPI.removeLocationUpdates(                mGoogleapiclient, this);    }    protected voID startLocationUpdates() {        Log.d(TAG, "Location update starting...");        LocationServices.FusedLocationAPI.requestLocationUpdates(                mGoogleapiclient, mLocationRequest, this);    }    private voID openDB() {        dbAdapter = new DBAdapter(this);        dbAdapter.open();    }    private voID closeDB() {        dbAdapter = new DBAdapter(this);        dbAdapter.close();    }    protected voID createLocationRequest() {        Log.e(TAG, " ***** Creating location request");        mLocationRequest = new LocationRequest();        mLocationRequest.setInterval(INTERVAL);        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);        mLocationRequest.setSmallestdisplacement(disPLACEMENT);    }    private boolean isGooglePlayServicesAvailable() {        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);        if (ConnectionResult.SUCCESS == status) {            return true;        } else {            Log.e(TAG, " ***** Update Google play service ");            return false;        }    }}

解决方法:

它对您不起作用的原因是您正在使用IntentService,因此调用stopService()不会导致调用onDestroy(),大概是因为在onHandleIntent()完成之后已经调用了它.无需在IntentService上调用stopService(),请参见here.

看来您应该只使用Service而不是IntentService.这样,当您调用stopService()时,它将按预期的那样调用onDestroy()并注销注册以进行位置更新.

您需要做的唯一其他更改是重写onStartCommand()而不是onHandleIntent().

您将使用类扩展Service而不是IntentService,然后将代码移动以注册位置更新到onStartCommand:

 @OverrIDe    public int onStartCommand(Intent intent, int flags, int startID) {        Log.e(TAG, " ***** Service on start command");        if (isGooglePlayServicesAvailable()) {            createLocationRequest();            mGoogleapiclient = new Googleapiclient.Builder(this)                    .addAPI(LocationServices.API)                    .addConnectionCallbacks(this)                    .addOnConnectionFailedListener(this)                    .build();            mGoogleapiclient.connect();        }        return Service.START_STICKY;    }

这样,您仍然可以调用startService()和stopService(),它应该可以按预期工作.

总结

以上是内存溢出为你收集整理的可以停止位置更新,Android服务全部内容,希望文章能够帮你解决可以停止位置更新,Android服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089610.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存