如何让AltBeacon只在用户登录的情况下在后台进行监控



我想在Android上试用iBeacon。看起来AltBeacon是最好的工具。我读到,如果我把我的应用程序的子类化为这个,我可以让我的应用工作,即使应用程序在之后被杀死。

如果我想让我的应用程序只在用户登录时进行监控,该怎么办?应用程序每次启动都会运行,不是吗?如何在登录后在应用程序中运行BootstrapNotifier,而在用户未登录的情况下不运行它?

        @Override
        protected void onPostExecute(final Boolean success) {
            if (success) {
                //algorithm to make altbeacon run in the background, even after the app killed
            } else {
                //if failed
            }
        }

因此,以下是建议的解决方案:

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
    // OTHER CODE
    public void onCreate() {
        super.onCreate();
        if (loggedin) {
            BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
            Region region = new Region("backgroundRegion", null, null, null);
            regionBootstrap = new RegionBootstrap(this, region);
            BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
        }
    }
    // OTHER CODE
}
public class LoginACtivity {
    // OTHER CODE
    public void onClick {
        if (username == TRUE && password == TRUE) {
            // SINCE THE USER LOGGED IN, HOW DO I MAKE MY APP TO START ALWAYS SCAN EVEN AFTER REBOOTING AS LONG THE USER ISN'T LOGGING OUT?
        }
    }
    // OTHER CODE
}
public class MainActivity {
    // OTHER CODE
    private void logout {
        // SINCE THE USER CLICK LOG OUT BUTTON, HOW DO I MAKE MY APP TO STOP SCANNING EVEN AFTER REBOOTING UNTIL THE USER LOGGING IN AGAIN?
    }
    // OTHER CODE
}

只有当用户登录时,即使在重新启动且应用程序未运行后,该代码是否能保证我的应用程序始终扫描信标?

您只需要包装构造RegionBootstrap对象的代码,就可以检查用户以前是否登录过。如果没有,就不要构建它。

然后,您可以在以后禁用RegionBootstrap,并在必要时对其进行重建。像这样:

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
  ...
  private RegionBootstrap regionBootstrap;
  public RegionBootstrap startBeaconMonitoring() {
    if (regionBootstrap == null) {
        Region region = new Region("backgroundRegion", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);          
    }
  }
  public RegionBootstrap stopBeaconMonitoring() {
    if (regionBootstrap != null) {
        regionBootstrap.disable();          
    }
  }
  public void onCreate() {
    super.onCreate();
    if (loggedin) {
       startBeaconMonitoring();
       ...
    }
}

public class MainActivity {
  ...
  private void logout() {
    ((BeaconReferenceApplication)this.getApplication()).stopBeaconMonitoring();
  }
  private void login() {
    ((BeaconReferenceApplication)this.getApplication()).startBeaconMonitoring();
  }
}

相关内容

  • 没有找到相关文章

最新更新