我正在使用startService(intent)
从MainActivity开始我的后台服务,该服务将用户的位置发送到服务器。我想让用户停止此服务(如果他不希望通过单击菜单中的按钮,则停止从onLocationChanged方法触发数据requestLocationUpdates
)。
我已经尝试了onOptionsItemSelected
方法中的代码,但是当我点击按钮时它仍然有效。
如何停止此服务?
MainActivity:
@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);
}
}
答案 0 :(得分:1)
看起来你现在编码的方式,你的服务并没有真正做任何事情。
因为您已将MyLocationListener
实例注册为LocationListener,所以停止服务不会做任何事情。
此外,由于您在stopSelf()
中致电onStartCommand()
,因此您每次启动时都会立即停止服务。
我建议您将服务作为LocationListener,而不要在stopSelf()
中致电onStartCommand()
。
另外,在服务中覆盖onDestroy()
,并明确调用removeUpdates()
,以确保您的应用释放它以保持GPS无线电有效的请求。
这样的事情:
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) {
}
}