2016-03-01 24 views
0

我正在使用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

創建服務......在服務監視信標。從服務生成通知並點擊通知打開活動。 –

+0

實際上,這個庫中有內置的服務支持,我可以實現它。我只是在通知功能方面有問題 – cryptic

+0

你在LogCat中看到了什麼「」進入區域「?」如果是這樣,那麼它會發生什麼? – davidgyoung

回答

0

可以使用setMonitoringListener和設置您的通知onEnteredRegion地區並退出

beaconManager.setMonitoringListener(newBeaconManager.MonitoringListener() 
    { 
    @Override 

    public void onEnteredRegion(Region region, List<Beacon> list) { 
     Calendar calendar = Calendar.getInstance(); 
     Date entertime = calendar.getTime(); 
     Log.d("Lalit", "Region Enter :- " + entertime); 
     Log.d("List", "Region UUID id :- "+region.getProximityUUID()); 
    } 

    @Override 
    public void onExitedRegion(Region region) { 
     Calendar calendar = Calendar.getInstance(); 
     Date entertime = calendar.getTime(); 
     Log.d("Lalit", "Region exit :- " + entertime); 
     Log.d("List", "Region UUID id :- "+region.getProximityUUID()); 
    } 
});