I have trouble with showing my app on region entered, I followed the sample here.
It works when I don't bind
the BeaconManager to my Activty, but when I do, I get the logs from the Activity not from the Application and the app doesn't show up although it's not visible.
So the question is, can I use a class extends Application implements BootstrapNotifier
for starting the app in background and a class extends AppCompatActivity implements BeaconConsumer
to handle monitoring/ranging or do I need to handle everything in the application class because binding it in the activity fails the background launch?
(Sorry for the bad code block, I just can't handle this stackoverflow code thing correctly)
public class BeaconActivity extends AppCompatActivity implements BeaconConsumer {
protected final String TAG = "BeaconActivity";
private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate()");
setContentView(R.layout.activity_beacon);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RangedBeacon.setSampleExpirationMilliseconds(8000);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "Unbind Beaconmanager");
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(final Region region) {
Log.d(TAG, "Enter Region identifier: " + region.getId1() + ", " + region.getId2() + ", " + region.getId3());
}
@Override
public void didExitRegion(final Region region) {
Log.d(TAG, "Exit Region identifier: " + region.getId1() + ", " + region.getId2() + ", " + region.getId3());
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
}
});
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (final Beacon beacon : beacons) {
final String distance = new DecimalFormat("##.######").format(beacon.getDistance());
Log.d(TAG, "Distance: " + distance + ". This beacon has identifiers:" + beacon.getId1() + ", " + beacon.getId2() + ", " + beacon.getId3());
}
}
});
try {
Region region = new Region("all", null, null, null);
beaconManager.startMonitoringBeaconsInRegion(all);
beaconManager.startRangingBeaconsInRegion(all);
} catch (RemoteException e) {
}
}
}
public class BeaconApplication extends Application implements BootstrapNotifier {
private static final String TAG = "BeaconApplication";
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("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
Region all = new Region("all", null, null, null);
regionBootstrap = new RegionBootstrap(this, all);
}
@Override
public void didEnterRegion(Region region) {
Log.d(TAG, "Enter region " + region.getUniqueId());
// regionBootstrap.disable();
Intent intent = new Intent(this, BeaconActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
@Override
public void didExitRegion(Region region) {
Log.d(TAG, "Exit region " + region.getUniqueId());
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
}
Edit: I think I might just screwed up a little. Explained here the app only launches if killed (not in task switcher anymore). If it's available in task switcher it just handles the incoming events in the background, that's why I get the activity logs.