【问题标题】:Android Stop a service form a serviceAndroid 停止服务形式的服务
【发布时间】:2018-10-18 11:53:49
【问题描述】:

我无法停止我的服务。

我有两个服务。 Service1 使用 startService(intent) 启动 Service2。 我用 stopSelf(); 停止 service1; 然后我在Service2中做一些事情并再次启动Service1并停止Service2。

所以他们总是重新开始。

第一次启动后服务的行为有所不同。

我写了一些日志消息,我可以看到这些信息在 log cat 中是乘数。

我还尝试使用 stopService 停止其他活动的 onStartCommand 中的活动。 (在Service1的onStartCommand中我调用stopService service2)

这是我的代码的一部分:

服务1:

public class Service1 extends Service{
    private int startId;

    @Override
    public void onCreate(){
        super.onCreate();   
    }

    private void doSomething(){
        ...
        ...
        Intent intent = new Intent(getApplicationContext(), Service2.class);
        startService(intent);
        this.stopSelf(startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.startId=startId;

        Intent beaconIntent = new Intent(getApplicationContext(), Service2.class);
        stopService(beaconIntent);

        doSomething();

        return START_STICKY;
    }
}

服务2:

public class Service2 extends Service implements BeaconConsumer, RangeNotifier{ 
    private BeaconManager mBeaconManager; 
    private int startId;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent service1 = new Intent(getApplicationContext(), Service1.class);
        stopService(service1);

        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
        mBeaconManager.bind(this);

        this.startId = startId;

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBeaconManager.unbind(this);
    }

    @Override
    public void onBeaconServiceConnect() {
        region = new Region("all-beacons-region", null, null, null);
        try {
            mBeaconManager.startRangingBeaconsInRegion(region);         
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        mBeaconManager.addRangeNotifier(this); 
    }

    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {        
        if(!beacons.isEmpty()){  
            doSomething...
            ...
            ...
            Intent intent = new Intent(getApplicationContext(), Service1.class);
            startService(intent);

            this.stopSelf();                
        }
    }
}

我做错了什么?

编辑:我没有提到我正在使用 altbeacon 库。我认为这不会产生任何影响。 但是当我查看启动应用程序时正在运行的服务时,在我停止 Service2 后,总是有两个 altbeacon 服务正在运行(BeaconIntentProcessor 和 BeaconService)。 也许他们多次调用我的服务。

【问题讨论】:

  • "What I'm doing worng?" 你没有提供你的来源
  • Service1的启动条件是什么?
  • 我添加了一些代码。 @Elias Fazel:Service1 的条件是什么意思?我在启动另一个服务后停止了这两个服务。所以它应该每次只运行一项服务。
  • 我建议将 stopSelft 替换为 stopService(new Intent(...));
  • 这不会改变任何事情。 Service2 仍在多次运行。我没有提到,我正在使用 altbeacon 库。我认为这不会产生任何影响。但是当我查看启动应用程序时正在运行的服务时,在我停止 Service2 后,总是有两个 altbeacon 服务正在运行(BeaconIntentProcessor 和 BeaconService)。也许他们多次调用我的服务......我将在我的问题中添加代码。

标签: android service altbeacon


【解决方案1】:

我知道我做错了什么。

问题不在于它本身的服务,而在于 altbeacon 库。更具体的 BeaconManager 仍在后台搜索信标,即使服务被销毁。 在日志中,看起来该服务正在运行多次。

解决方案不仅是取消绑定,还要停止 Ranging 并删除范围通知器。

mBeaconManager.stopRangingBeaconsInRegion(region);
mBeaconManager.removeAllRangeNotifiers();
mBeaconManager.unbind(this);

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2021-08-14
    • 2018-11-11
    • 2011-11-20
    相关资源
    最近更新 更多