检测到信标后无法启动通知

时间:2016-03-01 12:07:48

标签: android beacon

我正在使用Android Beacon Library来检测信标并启动一个简单的应用程序。 目前我能够启动信标服务,每5分钟(默认时间)后检测信标。但事情是每5分钟后我的应用程序才开始,而不给我移动状态栏中的通知。所需方案 - 之后检测时,屏幕上会出现通知,点击该通知即可开始活动。 以下是类代码,其中包含通知代码。

BeaconReferenceApplication

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {

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

    // By default the AndroidBeaconLibrary will only find AltBeacons.  If you wish to make it
    // find a different type of beacon, you must specify the byte layout for that beacon's
    // advertisement with a line like below.  The example shows how to find a beacon with the
    // same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb.  To find the proper
    // layout expression for other beacon types, do a web search for "setBeaconLayout"
    // including the quotes.
    //
    //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.setBackgroundScanPeriod(60000l);
    beaconManager.setBackgroundBetweenScanPeriod(60000l);
    try {
        beaconManager.updateScanPeriods();
    } catch (RemoteException e) {
        e.printStackTrace();
    }


    beaconManager.getBeaconParsers().
            add(new BeaconParser().
                    setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

    Log.d(TAG, "setting up background monitoring for beacons and power saving");
    // 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.d(TAG, "did enter region.");
    if (!haveDetectedBeaconsSinceBoot) {
        Log.d(TAG, "auto launching MainActivity");

        // The very first time since boot that we detect an beacon, we launch the
        // MainActivity
        Intent intent = new Intent(this, WebLayoutActivity.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.d(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("Please click on this notification to access discount")
                    .setSmallIcon(R.mipmap.ic_launcher);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, RangingActivity.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) {
    this.monitoringActivity = activity;
}

}

有谁能告诉我,这段代码有什么问题? 感谢

0 个答案:

没有答案