首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对背景中的Eddystone信标的反应

对背景中的Eddystone信标的反应
EN

Stack Overflow用户
提问于 2017-03-22 18:55:12
回答 1查看 286关注 0票数 1

我基本上是在尝试使用提供的第一个答案来完成。这个问题在网络上都有答案,但我很难让它发挥作用。有什么东西我需要启用,添加到我的清单,等等?我想对手机范围内的信标做出反应。我在Android工作,目标是Android4.3,并使用Android灯塔图书馆。根据他们的文档,我所需要的只是实现BootstrapNotifier,设置一个Region,然后每当它扫描一个信标时,它就会自动调用didEnterRegion。我的区域是Region region = new Region("all-beacons-region", null, null, null);

我还建立了一个非常简单的应用程序,扫描和发现在前景信标。因此,没有问题,我肯定能够拿起我的信标,并从他们的基本信息。

我的主要活动如下:

代码语言:javascript
复制
package com.example.justin.backgroundscantest;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }

我的课看起来是这样的:

代码语言:javascript
复制
import android.app.Application;
import android.content.Intent;
import android.util.Log;

import com.example.justin.backgroundscantest.MainActivity;

import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Region;

public class TestApp extends Application implements BootstrapNotifier {
    private static final String TAG = ".TestApp";
    private RegionBootstrap regionBootstrap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
        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 any beacon is seen (you can specify specific id filers in the parameters below)
        Region region = new Region("com.example.myapp.boostrapRegion", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);
    }

    @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);
    }
}

(编辑)最后,AndroidManifest.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.justin.backgroundscantest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

这一切都感觉很简单,这就是我很好奇我是不是错过了一些简单或愚蠢的东西。我已经在网上的几个地方找到了这个例子,但是没有提到我可能错过的任何东西。我有一个相当扎实的编码背景,但对Android还是新手,对Eddystone/BLE技术非常陌生。为了澄清实际的问题:当我把手机移到信标的范围内时,我的应用程序没有任何反应。我的期望是,它将“醒来”并开始MainActivity。我肯定在信标的范围内,信标一定开着,而且我也能在前景中扫描它。它只是不会唤醒我的应用程序。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-22 20:41:56

在创建像TestApp这样的定制Android TestApp类时,必须在清单中使用name属性声明它。如下所示:

代码语言:javascript
复制
<application
    android:name="TestApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

如果不这样做,Android将不会使用自定义的TestApp类,而是默认为其内置的基类Application类,从而不会执行任何TestApp代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42960386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档