我无法阻止我的服务。Andorid停止服务形式的服务
我有两项服务。 Service1使用startService(intent)启动Service2。 我用stopSelf()停止service1; 然后,我在Service2中做了一些事情,并再次启动Service1并停止Service2。
因此,他们总是再次相互启动。
第一次启动后,服务的行为是不同的。
我写了一些日志消息,我可以看到信息是乘以日志猫的倍数。
我也尝试用stopService停止其他活动的onStartCommand中的活动。 (在服务1的onStartCommand我打电话stopService服务2)
这是我的部分代码:
服务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)。他们可能会打电话给我的服务mutlipy时间。
感谢您的帮助
问候
''我在做什么worng?''你没有定位你的消息来源 – pskink
什么是启动Service1的条件? –
我添加了一些代码。 @Elias Fazel:你对Service1的条件有什么意义?在我启动其他服务后,我停止了这两项服务。所以它每次只能运行一个服务。 – Fabian