我有來自Radius網絡的2個Rad信標。我使用Locate應用程序將它們配置爲Eddystone。現在我寫了一個小程序在後臺發送通知,即應用程序未運行時。我需要在應用程序處於後臺時發送通知。我正在使用Android信標庫來實現此目的。我嘗試了幾乎所有的鏈接,但我無法檢測到它。無法在Android的背景中檢測到我的信標
我在這裏
public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier {
private static final String TAG = "BeaconReferenceApp";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private MonitoringActivity monitoringActivity = null;
public void onCreate() {
super.onCreate();
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.setBackgroundBetweenScanPeriod(1000);
Log.i(TAG, "setting up background monitoring for beacons and power saving");
//Toast.makeText(getApplicationContext(), "Called!!!" , Toast.LENGTH_LONG).show();
// wake up the app when a beacon is seen
Region region = new Region("backgroundRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
// simply constructing this class and holding a reference to it in your custom Application
// class will automatically cause the BeaconLibrary to save battery whenever the application
// is not visible. This reduces bluetooth power usage by about 60%
//backgroundPowerSaver = new BackgroundPowerSaver(this);
// If you wish to test beacon detection in the Android Emulator, you can use code like this:
// BeaconManager.setBeaconSimulator(new TimedBeaconSimulator());
// ((TimedBeaconSimulator) BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
}
@Override
public void didEnterRegion(Region arg0) {
// In this example, this class sends a notification to the user whenever a Beacon
// matching a Region (defined above) are first seen.
Log.i(TAG, "did enter region.");
//sendNotification();
if (!haveDetectedBeaconsSinceBoot) {
Log.i(TAG, "auto launching MainActivity");
// The very first time since boot that we detect an beacon, we launch the
// MainActivity
Intent intent = new Intent(this, MonitoringActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Important: make sure to add android:launchMode="singleInstance" in the manifest
// to keep multiple copies of this activity from getting created if the user has
// already manually launched the app.
this.startActivity(intent);
haveDetectedBeaconsSinceBoot = true;
} else {
if (monitoringActivity != null) {
// If the Monitoring Activity is visible, we log info about the beacons we have
// seen on its display
monitoringActivity.logToDisplay("I see a beacon again");
} else {
// If we have already seen beacons before, but the monitoring activity is not in
// the foreground, we send a notification to the user on subsequent detections.
Log.i(TAG, "Sending notification.");
//sendNotification();
}
}
}
@Override
public void didExitRegion(Region region) {
if (monitoringActivity != null) {
monitoringActivity.logToDisplay("I no longer see a beacon.");
}
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
if (monitoringActivity != null) {
monitoringActivity.logToDisplay("I have just switched from seeing/not seeing beacons: " + state);
}
}
private void sendNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Beacon Reference Application")
.setContentText("An beacon is nearby in application.")
.setSmallIcon(R.drawable.ic_launcher);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.class));
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
public void setMonitoringActivity(MonitoringActivity activity) {
Log.i("Log", "TEST ONLY");
this.monitoringActivity = activity;
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
// TODO Auto-generated method stub
sendNotification();
//Log.i("Log", "TEST ONLY");
}
更容易爲人們幫助你,如果他們有一些相關的**一段代碼**上工作 - 請告訴我們什麼你已嘗試到目前爲止 – 0X0nosugar
我更新了我的代碼。請讓我知道是否可以做任何事情。 – Sangie
我還沒有經驗的信標(還),對不起。但是我可以看到一方面使用NotificationCompat.Builder,另一方面使用NotificationManager(而不是NotificationManagerCompat)。那樣有用嗎? – 0X0nosugar