Pat*_*ick 2 android google-play-services eddystone
借助谷歌新的Eddystone标准,他们将在Google Play服务附近的Api中为Android提供支持.我们可以注册eddystone信标,即使应用程序没有运行,我们的应用程序也会收到意图吗?
是的,使用完全支持Eddystone的Android Beacon Library可以做到这一点.
应用程序的后台启动机制在Eddystone上的工作方式与对库支持的其他类型信标的工作方式相同.您RegionBootstrap在自定义Application类中使用对象.您可以在此处阅读有关此方法的详细信息.
与Eddystone的唯一区别是你必须设置一个BeaconParser解码Eddystone-UID框架,然后设置一个Region匹配你的Eddystone命名空间ID:
public class MyApplicationName extends Application implements BootstrapNotifier {
private static final String TAG = ".MyApplicationName";
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
// Detect the main identifier (UID) frame:
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
// wake up the app when a beacon matching myEddystoneNamespaceId is seen
myEddystoneNamespaceId = Identifier.parse("0x2f234454f4911ba9ffa6");
Region region = new Region("com.example.myapp.boostrapRegion", myEddystoneNamespaceId, null, null);
regionBootstrap = new RegionBootstrap(this, region);
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// Don't care
}
@Override
public void didEnterRegion(Region arg0) {
Log.d(TAG, "Got a didEnterRegion call");
// This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
// if you want the Activity to launch every single time beacons come into view, remove this call.
regionBootstrap.disable();
Intent intent = new Intent(this, MainActivity.class);
// IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
// created when a user launches the activity manually and it gets launched from here.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
@Override
public void didExitRegion(Region arg0) {
// Don't care
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3002 次 |
| 最近记录: |