我正在使用altbeacon lib 来检测 ibeacon。如果我通过启动服务从活动中检测到 ibeacon,则它的检测 ibeacon 非常好。
现在我想在后台启动相同的操作(STICKY 服务)。我遵循了这个文件。它正在检测 ibeacons,但问题在于时间。检测时间过长。我一直在检测如下。
public class Ibeaconpocsuper extends Application implements BootstrapNotifier {
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
init();
}
private void init() {
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.setBackgroundScanPeriod(1100l);
beaconManager.setBackgroundBetweenScanPeriod(1100l);
Region region = new Region("com.exe", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// TODO Auto-generated method stub
}
@Override
public void didEnterRegion(Region arg0) {
showNotification() ;
}
@Override
public void didExitRegion(Region arg0) {
// TODO Auto-generated method stub
}
private void showNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("My notification")
.setContentText("Contentttttttt").setAutoCancel(true);
Intent intent = new Intent(this, IbeaconNotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}