【问题标题】:Estimote iBeacon: Monitoring in background (Android)Estimote iBeacon:后台监控 (Android)
【发布时间】:2026-01-18 01:25:01
【问题描述】:

我希望在我的应用打开但处于后台时收到推送通知。现在我已经更改了 Estimote Demo,当我的应用程序处于前台时,我的应用程序会给我一个通知,这并没有多大用处。 我在这里发布了我的 NotifyDemoActivity 类的代码,一旦我打开应用程序就会调用它

public class NotifyDemoActivity extends Activity {
  private static final String TAG = NotifyDemoActivity.class.getSimpleName();
  private static final int NOTIFICATION_ID = 123;
  private BeaconManager beaconManager;
  private NotificationManager notificationManager;
  private Region region;
  private long[] mVibratePattern = { 0, 200, 200, 300 };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notify_demo);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    beacon.getMinor());
    region = new Region("rid", null, null, null);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    beaconManager = new BeaconManager(this);

    beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);

    beaconManager.setMonitoringListener(new MonitoringListener() {
      @Override
      public void onEnteredRegion(Region region, List<Beacon> beacons) {
        postNotification("Entered region");
      }

      @Override
      public void onExitedRegion(Region region) {
        postNotification("Exited region");
      }
    });
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      finish();
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onResume() {
    super.onResume();
    notificationManager.cancel(NOTIFICATION_ID);
    beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
      @Override
      public void onServiceReady() {
        try {
          beaconManager.startMonitoring(region);
        } catch (RemoteException e) {
          Log.d(TAG, "Error while starting monitoring");
        }
      }
    });
  }

  @Override
  protected void onDestroy() {
    notificationManager.cancel(NOTIFICATION_ID);
    beaconManager.disconnect();
    super.onDestroy();
  }

  private void postNotification(String msg) {
    Intent notifyIntent = new Intent(NotifyDemoActivity.this, NotifyDemoActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(
      NotifyDemoActivity.this,
      0,
      new Intent[]{notifyIntent},
      PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(NotifyDemoActivity.this)
    .setSmallIcon(R.drawable.beacon_gray)
    .setContentTitle("Notify Demo")
    .setContentText(msg)
    .setAutoCancel(true)
    .setContentIntent(pendingIntent)
    .setVibrate(mVibratePattern)
    .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notificationManager.notify(NOTIFICATION_ID, notification);

    TextView statusTextView = (TextView) findViewById(R.id.status);
    statusTextView.setText(msg);
  }
}

【问题讨论】:

  • 来自 Estimote 网站的信息:iOS 7 iBeacon 即使应用程序关闭也能正常工作 本周 Apple 推出了 iOS 7.1 的稳定版本。新版本为所有使用 Beacons 的人带来了一些非常令人兴奋的变化。即:应用程序不必再继续在后台运行才能向用户发送通知。在 estimote 社区论坛上查看更多信息。
  • @ayasha 你能加入我吗Here,我需要一些关于 Estimote 的指导。

标签: android background ibeacon estimote


【解决方案1】:

你应该在你的应用程序类中而不是在活动中持有BeaconManager

Activity 将停止、销毁,BeaconManager 将停止监控。另一方面,应用程序仍将保持引用并继续监控。

当在监视您的应用程序类时发现信标时,可以发布通知。当用户决定点击它时,它会触发一些活动。

【讨论】:

  • 谢谢!现在可以了! ..我现在有一个与测距/监控相关的新问题 :) 你知道每次用户进入新区域时都会收到通知时该怎么做吗?也就是说,如果我的蓝色信标更近,我希望收到某个通知,但如果绿色信标更近,我希望收到另一个通知。我不明白这是否应该在监控或测距中,我认为测距但我不明白在哪里..
  • 监控在这里是不错的选择。只需找到信标的标识符并开始监控与您的三个信标相对应的三个区域。
  • 你指的应用类是什么?现在我在源文件夹中的所有 java 文件都作为活动文件
  • 我在我的代码中使用了信标发现的函数,我调用了这个函数,但它在 beaconmanager.setMonitoringListener 行停止并退出了这个函数。
【解决方案2】:

你应该创建服务。

在你的代码中你应该做一些改变。

之后

beaconManager = new BeaconManager(this);

你应该启动“信标服务”

      if (!beaconManager.isBluetoothEnabled()) {
          stopSelf();
      }

          beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
              @Override
              public void onServiceReady() {
                try {
                  beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
                } catch (RemoteException e) {
                  Log.e("error", "Cannot start ranging", e);
                }
              }
          });

同时检查您设备上的蓝牙是否处于活动状态。

在程序 beaconManager.setMonitoringListener(new MonitoringListener() 添加命令以创建通知。

【讨论】: