gpt4 book ai didi

android - 停止包含 LocationManager 的后台服务

转载 作者:行者123 更新时间:2023-11-30 02:01:28 25 4
gpt4 key购买 nike

我使用 startService(intent) 从 MainActivity 启动我的后台服务,它将用户的位置发送到服务器。如果用户不希望通过单击菜单中的按钮来停止此服务(也停止从 onLocationChanged 方法触发数据的 requestLocationUpdates)。我已经尝试了 onOptionsItemSelected 方法中的代码,但当我单击按钮时它仍然有效。

我怎样才能停止这个服务?

主要 Activity :

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route_available);

Intent i = new Intent(this, TrackingService.class);
startService(i);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.disconnect_id:
System.out.println("test onCreateOptionsMenu was invoked.");

// Stop the service when the Menu button clicks.
Intent i = new Intent(this, TrackingService.class);
stopService(i);

return true;

default:
return super.onOptionsItemSelected(item);
}
}

跟踪服务类:

public class TrackingService extends Service {

....
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

Toast.makeText(this, "Location Updation has started", Toast.LENGTH_LONG)
.show();
Log.v("X", "Response:in onStartCommand()");

detectLocation();
stopSelf();
return START_STICKY;
}

private void detectLocation() {
// TODO Auto-generated method stub
Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT)
.show();

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new MyLocationListener(this);
// location updates: at least 0 meter and 60 seconds change.
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
ll);

Log.v("X", "Response:After lm1.requestLocationUpdates ");
enableGPS(lm);

}


}

最佳答案

它看起来像您现在的编码方式,您的服务实际上并没有做任何事情。

因为您已将您的 MyLocationListener 实例注册为 LocationListener,所以停止服务不会执行任何操作。

此外,由于您在 onStartCommand() 中调用了 stopSelf(),因此每次服务启动时您都会立即停止该服务。

我建议将服务设为 LocationListener,并且不要在 onStartCommand() 中调用 stopSelf().此外,覆盖服务中的 onDestroy(),并显式调用 removeUpdates() 以确保您的应用发布保持 GPS radio 处于 Activity 状态的请求。

像这样:

public class TrackingService extends Service implements LocationListener{

LocationManager lm; //make instance variable

//....
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

Toast.makeText(this, "Location Updation has started", Toast.LENGTH_LONG)
.show();
Log.v("X", "Response:in onStartCommand()");

detectLocation();
//stopSelf(); //don't stop the service here
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
lm.removeUpdates(this);
}

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


private void detectLocation() {
// TODO Auto-generated method stub
Toast.makeText(this, "Inside detectlocation()", Toast.LENGTH_SHORT)
.show();

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//LocationListener ll = new MyLocationListener(this);

// location updates: at least 0 meter and 60 seconds change.
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
this);

Log.v("X", "Response:After lm1.requestLocationUpdates ");
enableGPS(lm);

}

@Override
public void onLocationChanged(Location location) {
//put your location changed code here

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

关于android - 停止包含 LocationManager 的后台服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31389597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com