Android 我可以使用后台服务来监视iBeacon吗?

Android 我可以使用后台服务来监视iBeacon吗?,android,ibeacon,estimote,Android,Ibeacon,Estimote,我尝试创建一个IntentService,以在后台监视estimote信标。 在活动上启动服务后,它会抛出错误 Service com.estimote.examples.demos.scanbeaconservatice已泄漏ServiceConnection com.estimote.sdk.BeaconManager$InternalServiceConnection@48523f80最初绑定在此处的 我的ScanBean服务类: public class ScanBeaconServic

我尝试创建一个
IntentService
,以在后台监视estimote信标。 在
活动
上启动服务后,它会抛出错误

Service com.estimote.examples.demos.scanbeaconservatice已泄漏ServiceConnection com.estimote.sdk.BeaconManager$InternalServiceConnection@48523f80最初绑定在此处的

我的ScanBean服务类:

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);
    }
}
公共类ScanBeaconService扩展了IntentService{
私人BeaconManager BeaconManager;
int通知_ID=111111;
通知经理通知经理;
私有静态最终区域所有\估计\信标\区域=新区域(“rid”,null,null,null);
公共图书馆(){
超级(“ScanBeaconService”);
}
公共扫描(字符串名称){
超级(姓名);
}
@凌驾
受保护的手部内容无效(意图){
Log.d(getClass().getName(),“启动服务扫描”);
com.estimote.sdk.utils.L.enableDebugLogging(true);
notificationManager=(notificationManager)getSystemService(Context.NOTIFICATION\u服务);
beaconManager=新的beaconManager(此);
如果(!beaconManager.hasBluetooth()){
通知后(“没有bt”);
}如果(!beaconManager.isBluetoothEnabled()){
通知后(“bt未启用”);
}
connect(新的beaconManager.ServiceReadyCallback(){
@凌驾
服务日上的公共无效(){
通知后(“信标管理器就绪”);
}
});
beaconManager.setMonitoringListener(新建MonitoringListener(){
@凌驾
公共区域(区域、列表信标){
通知后(“输入区域”);
}
@凌驾
公共无效onExitedRegion(区域){
通知后(“退出区域”);
}
});
试一试{
信标管理器。开始监控(所有预计信标区域);
}捕获(远程异常){
Log.d(getClass().getName(),“启动监视时出错”);
}   
}
私有void postNotification(字符串msg){
Intent notifyIntent=新的Intent(这是NotifyDemoActivity.class);
notifyIntent.setFlags(Intent.FLAG\u ACTIVITY\u SINGLE\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivities(
这
0,
新意图[]{notifyIntent},
PendingEvent.FLAG_UPDATE_CURRENT);
通知通知=新建通知.Builder(此)
.setSmallIcon(R.可绘制.灯塔灰)
.setContentTitle(“通知演示”)
.setContentText(msg)
.setAutoCancel(真)
.setContentIntent(挂起内容)
.build();
notification.defaults |=notification.DEFAULT_声音;
notification.defaults |=notification.DEFAULT_灯;
notificationManager.notify(通知ID,通知);
}
}

如果您想在后台连续执行某些操作,请使用Service not IntentService。@Gabeschen谢谢您的评论,我会尝试一下