我尝试在背景中创建一个IntentService
来监控estimote信标。
在Activity
上启动服务后,它会抛出错误
Service com.estimote.examples.demos.ScanBeaconService has leaked ServiceConnection com.estimote.sdk.BeaconManager$InternalServiceConnection@48523f80 that was originally bound here
我的ScanBeanService类:
public class ScanBeaconService extends IntentService {
private BeaconManager beaconManager;
int NOTIFICATION_ID = 111111;
NotificationManager notificationManager;
private static final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("rid", null, null, null);
public ScanBeaconService() {
super("ScanBeaconService");
}
public ScanBeaconService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(getClass().getName(), "start service ScanBeaconService");
com.estimote.sdk.utils.L.enableDebugLogging(true);
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);
if (!beaconManager.hasBluetooth()) {
postNotification("don't have bt");
} else if (!beaconManager.isBluetoothEnabled()) {
postNotification("bt not enable");
}
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
postNotification("beacon manager ready");
}
});
beaconManager.setMonitoringListener(new MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
postNotification("Entered region");
}
@Override
public void onExitedRegion(Region region) {
postNotification("Exited region");
}
});
try {
beaconManager.startMonitoring(ALL_ESTIMOTE_BEACONS_REGION);
} catch (RemoteException e) {
Log.d(getClass().getName(), "Error while starting monitoring");
}
}
private void postNotification(String msg) {
Intent notifyIntent = new Intent(this, NotifyDemoActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
this,
0,
new Intent[]{notifyIntent},
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.beacon_gray)
.setContentTitle("Notify Demo")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
}
}