首页 文章

自定义服务和AltBeacon

提问于
浏览
0

抱歉我的英语我'm working with iBeacon, and to do that, i'm使用Android Beacon Library . 这很棒,工作得很好,但现在我需要你的帮助 .

我有一个线程,当某个iBeacon进入该区域时启动并发送信息,并在iBeacon离开该区域时停止 . 问题是,当我杀死应用程序时,线程死了 . 我想一个服务,但我发现使用BootstrapNotifier是不可能使用另一个自定义服务 .

那么,对于如何完成这项任务,您有什么想法吗?提前感谢您的建议 .

1 回答

  • 1

    好的,我以另一种方式解决了它 . 我正在使用我的自定义服务,并且不再在Application中实现BootstrapNotifier .

    这是我的代码,如果有人需要它 .

    public class BeaconDetector extends Service implements BeaconConsumer {
    
    private static final String TAG = "BeaconDetector";
    
    private BeaconUtility.BeaconObject beaconObject;
    
    private Context getServiceCtx(){
        return BeaconDetector.this;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        IMLog.e(TAG, "Created.");
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        IMLog.e(TAG, "Start.");
    
        beaconObject = BeaconUtility.instantiateBeaconManager(this);
        beaconObject.beaconManager.bind(this);
    
        return START_STICKY;
    }
    
    @Override
    public void onDestroy() {
        IMLog.e(TAG, "Destroy.");
        beaconObject.beaconManager.unbind(this);
    }
    
    
    @Override
    public void onBeaconServiceConnect() {
        IMLog.e(TAG, "Connected.");
    
        beaconObject.beaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region arg0) {
                // In this example, this class sends a notification to the user whenever a Beacon
                // matching a Region (defined above) are first seen.
                IMLog.e(TAG, "did enter region.");
                Sender.getInstance(getServiceCtx()).startSender();
            }
    
            @Override
            public void didExitRegion(Region region) {
                IMLog.e(TAG, "did exit region.");
                if (Sender.getInstance(getServiceCtx()).isAlive()) {
                    Sender.getInstance(getServiceCtx()).stopSender();
                }
            }
    
            @Override
            public void didDetermineStateForRegion(int state, Region region) {
                IMLog.e(TAG, "did enter region.");
            }
        });
    
        try {
            beaconObject.beaconManager.startMonitoringBeaconsInRegion(BeaconUtility.getMonitoringRegion());
        } catch (RemoteException e) {
            IMLog.e(TAG, "Remote Exception.");
        }
    
    }
    

    }

相关问题