我建立了一个小应用程序,当用户按照本教程启动应用程序时,该应用程序使用Android信标库查找附近的信标:http : //www.software7.com/blog/creating-a-beacon-app-for-android-从零开始不到10分钟的时间/
如果该应用程序是startet,并且找到了属于特定区域的信标,则它将在控制台以及UI的文本标签上打印信息。
我正在使用我的OnCreate和OnDestroy方法来启动和关闭Beacon Manager ,该方法正在管理搜索过程。
第一次启动时,该应用程序运行良好。但是,当我用手机上的后退箭头按钮将其关闭并再次打开时,文本标签上将不再有控制台消息或更新。当屏幕进入睡眠状态并且我必须再次解锁它时,也会发生这种情况-没有控制台更新,也没有UI更新。
根据控制台消息,OnBeaconServiceConnect正在继续恢复该应用程序,但是我没有再从didEnterRegion或setRangeNotifier得到日志消息。
我还尝试将绑定和取消绑定放入OnPause和OnResume中,但效果也不尽人意。
如何再次正确恢复搜索和查找过程?感谢您的任何帮助 :)
如果您想简短看一下,这是我的注释代码:
package de.mediatoni.beacontut01;
import android.graphics.Region;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Identifier;
import org.altbeacon.beacon.MonitorNotifier;
import org.altbeacon.beacon.RangeNotifier;
import java.util.Collection;
public class BeaconActivity extends ActionBarActivity implements BeaconConsumer{
public static final String TAG = "BeaconsEverywhere";
// Beacon Manager Variable
private BeaconManager beaconManager;
//GUI Text Label
TextView rangeElement;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beacon);
// Vars for XML Elements
rangeElement = (TextView) findViewById(R.id.range);
// Instantiate a Beacon Manager via factory method
beaconManager = BeaconManager.getInstanceForApplication(this);
// Tell Library how to decode the signal
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
// Start the Beacon Manager
beaconManager.bind(this);
}
// When the Beacon Service starts, search for Beacons with the following Identifier
@Override
public void onBeaconServiceConnect() {
final org.altbeacon.beacon.Region region = new org.altbeacon.beacon.Region("myBeacons", Identifier.parse("73676723-7400-0000-ffff-0000ffff0005"), null, null);
beaconManager.setMonitorNotifier(new MonitorNotifier() {
// If the phone enters a Beacon region
@Override
public void didEnterRegion(org.altbeacon.beacon.Region region) {
try {
Log.d(TAG, "did Enter Region");
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
// If the phone leaves a Beacon region
@Override
public void didExitRegion(org.altbeacon.beacon.Region region) {
try {
Log.d(TAG, "did Exit Region");
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int i, org.altbeacon.beacon.Region region) {
}
});
// If the phone finds a Beacon fitting the rules, print it in the console
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
//Log out welche beacons in der Nähe sind
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
for(final Beacon oneBeacon : beacons) {
Log.d(TAG, "distance: " + oneBeacon.getDistance() + "id: " + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
// Access UI thread and make changes to the UI
// Placing rangeElement.setText outside of the Runnable will crash the App
runOnUiThread(new Runnable() {
@Override
public void run() {
// Change the text label in the UI
rangeElement.setText(String.valueOf(oneBeacon.getDistance()));
}
});
}
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
// When the App gets closed, stop the Beacon Manager
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_beacon, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
问题可能是活动重新启动时您没有收到第二个进入事件,因为该信标仍然可见并且永不消失。从库版本2.8开始,除非有相应的退出事件,否则重复输入事件将被禁止。
为了在每次开始监视时获取进入事件,只需在再次开始监视之前停止监视区域即可。像这样:
beaconManager.stopMonitoringBeaconsInRegion(region);
beaconManager.startMonitoringBeaconsInRegion(region);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句