Java 对背景中的埃迪斯通信标做出反应
我基本上是在尝试完成,使用提供的第一个答案。这个问题在网上都有答案,但我很难让它发挥作用。是否有我需要启用、添加到清单等的内容。?我想对手机范围内的信号灯做出反应。我在安卓工作室工作,以安卓4.3为目标,并使用。根据他们的文档,我只需要实现Java 对背景中的埃迪斯通信标做出反应,java,android,beacon,eddystone,Java,Android,Beacon,Eddystone,我基本上是在尝试完成,使用提供的第一个答案。这个问题在网上都有答案,但我很难让它发挥作用。是否有我需要启用、添加到清单等的内容。?我想对手机范围内的信号灯做出反应。我在安卓工作室工作,以安卓4.3为目标,并使用。根据他们的文档,我只需要实现BootstrapNotifier,设置一个区域,然后每当它扫描一个信标时,它就会自动调用didEnterRegion。我的区域是Region Region=新区域(“所有信标区域”,null,null,null) 我还构建了一个非常简单的应用程序,可以扫描并
BootstrapNotifier
,设置一个区域
,然后每当它扫描一个信标时,它就会自动调用didEnterRegion
。我的区域是Region Region=新区域(“所有信标区域”,null,null,null)代码>
我还构建了一个非常简单的应用程序,可以扫描并找到前景中的信标。所以没有问题,我肯定能够拿起我的信标并从中提取基本信息
我的主要活动如下:
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);
}
}
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);
}
}
我的班级是这样的:
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);
}
}
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:
<?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技术非常陌生。为了澄清实际问题:当我将手机移动到信标的范围内时,我的应用程序没有任何反应。我的期望是它会“醒来”并开始活动。我肯定在信标的范围内,信标肯定是开着的,而且我可以在前景中扫描它。它不会唤醒我的应用程序。谢谢 创建自定义Android应用程序
类(如TestApp
)时,必须在清单中使用名称
属性声明它。像这样:
<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
类,而是默认使用其内置的基础应用程序
类,导致所有TestApp
代码都无法执行。您能显示您的AndroidManifest.xml吗?编辑了问题,因为我正在查看更多信息,我是否必须在清单中定义TestApp?我想知道问题是否在于我不完全清楚如何编写一个扩展应用程序的类。是的!真的只是我没有很好地理解Android的工作原理。谢谢