Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 停止包含LocationManager的后台服务_Android - Fatal编程技术网

Android 停止包含LocationManager的后台服务

Android 停止包含LocationManager的后台服务,android,Android,我使用startService(intent)从主活动启动后台服务,它将用户的位置发送到服务器。如果用户不希望,我希望通过单击菜单中的按钮,使其能够停止此服务(同时停止从onLocationChanged方法触发数据的requestLocationUpdates)。 我已经尝试了onOptionsItemSelected方法中的代码,但是当我单击按钮时,它仍然可以工作 我怎样才能停止这项服务 main活动: @Override protected void onCreate(Bundle

我使用
startService(intent)
从主活动启动后台服务,它将用户的位置发送到服务器。如果用户不希望,我希望通过单击菜单中的按钮,使其能够停止此服务(同时停止从onLocationChanged方法触发数据的
requestLocationUpdates
)。 我已经尝试了
onOptionsItemSelected
方法中的代码,但是当我单击按钮时,它仍然可以工作

我怎样才能停止这项服务

main活动:

    @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);

    }


}
跟踪服务类别:

    @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无线电活动的请求

大概是这样的:

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) {

    }

}

但是如何从Main活动中的
OnOptions ItemSelected()
调用TrackingService中的
onDestroy()
?我必须将其作为菜单的一个选项来实现。@light
ondestory()
将在您调用
stopService(I)
时被调用,因此通过对您的服务进行这些更改,您应该能够保持您的MainActivity代码与您的问题相同。至于
LocationListener ll
,它是不需要的,因为您只需在服务中使用
作为LocationListener引用。是否有建议的方法在停止服务后重新启动服务?@light,您只需调用
startService()
,就像您当前在
onCreate()
中所做的那样,如果您想让用户选择重新开始位置跟踪,您可以从另一个菜单项执行此操作。(或者,您可以将一个菜单项更改为开关,并在用户每次选择启动或停止位置跟踪时更改文本)。请看一下这个问题?