【问题标题】:Starting the App when an iBeacon is detected检测到 iBeacon 时启动应用程序
【发布时间】:2014-08-20 07:25:18
【问题描述】:

我正在试验 Android Beacon 库以使用以下代码在后台监控 iBeacon:

public class IBeaconBootstrap extends Application implements BootstrapNotifier {

private RegionBootstrap regionBootstrap;

@Override
public void onCreate() {

   super.onCreate();

   Log.d("IBeaconBootstrap", "App started up");

   // wake up the app when any beacon is seen (you can specify specific id
   // filers in the parameters below)

   Region region = new Region("MyRegion", null, null, null);
   regionBootstrap = new RegionBootstrap(this, region);

   // This is for Apple compatible iBeacons
   BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new     BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
}

@Override
public void didDetermineStateForRegion(int state, Region region) {

   Log.d("Boostrap didDetermineStateForRegion", "Region " + region.toString());
}

@Override
public void didEnterRegion(Region region) {

   Log.d("Boostrap didEnterRegion", "Got a didEnterRegion call");

   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   this.startActivity(intent);
}

@Override
public void didExitRegion(Region region) {

   Log.d("Boostrap didExitRegion", "Got a didExitRegion call");
}
}

当检测到 iBeacon(如果它正在运行)时,应用程序会进入前台,但如果应用程序未运行,则不会发生任何事情。这是预期的行为还是应用程序在未运行时应该启动?

【问题讨论】:

    标签: android ibeacon android-ibeacon altbeacon


    【解决方案1】:

    您可能需要澄清“应用程序未运行”的含义。你的意思是:

    1. 应用已安装但从未启动
    2. 该应用已启动一次,但已重新启动
    3. 应用程序已从任务切换器中终止

    使用上面的代码,这是每种情况下的预期行为:

    1. 应用将不会运行,也无法自动启动 Activity。
    2. 应用程序将在启动后定期开始扫描信标,并在检测到信标时启动 Activity。
    3. 应用程序不会运行,也无法自动启动,直到充电器连接/断开或重新启动。在那之后,行为如(2)所示。有关此案例的更多详细信息,请访问here.

    重要的是要注意,当没有可见的活动时,库只会每 5 分钟扫描一次信标以查找信标,因此检测可能需要很长时间。此间隔是完全可配置的。

    情况 (3) 的限制是由 Android 操作系统施加的。必须发生一个事件,允许在被用户杀死后重新启动应用程序。

    【讨论】:

    • 感谢您的及时答复。我对场景 #3 感兴趣。用户运行应用程序,使用它,然后杀死它。如果我理解你的答案,一旦应用程序被杀死,它就不再启动,除非用户插入/拔下电源或重新启动设备。对?换句话说,为了让应用程序持续感知 iBeacons,它必须在后台运行。当一个 ibeacon 在范围内时,它可以自动被带到前台。
    • 正确。我已经更新了我的答案,并提供了一个更详细解释其工作原理和原因的链接:altbeacon.github.io/android-beacon-library/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多