我使用altbeacon lib来检测ibeacons。如果我通过启动服务来检测来自活动的ibeacons,它的检测ibeacons就完全没问题了。
现在我想在后台启动相同的操作(STICKY服务)。我关注了this文件。 它正在检测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());
}
}