Estimote iBeacon:在后台监视(安卓) [英] Estimote iBeacon: Monitoring in background (Android)

查看:425
本文介绍了Estimote iBeacon:在后台监视(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有推送通知时,我的应用程序是开放的,但在后台。现在我已经改变了Estimote演示,和我的应用程序给了我一个通知时,我的应用程序是在前台是没有多大用处的。 我在这里发表NotifyDemoActivity类的我的code这是当我打开应用程序名为

 公共类NotifyDemoActivity延伸活动{
  私有静态最后字符串变量= NotifyDemoActivity.class.getSimpleName();
  私有静态最终诠释NOTIFICATION_ID = 123;
  私人BeaconManager beaconManager;
  私人NotificationManager notificationManager;
  专用区域区域;
  私人长[] mVibratePattern = {0,200,200,300};

  @覆盖
  保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.notify_demo);
    getActionBar()setDisplayHomeAsUpEnabled(真)。

    beacon.getMinor());
    地区=新区域(RID,NULL,NULL,NULL);
    notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    beaconManager =新BeaconManager(本);

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

    beaconManager.setMonitoringListener(新MonitoringListener(){
      @覆盖
      公共无效onEnteredRegion(区区域,列表和LT;灯塔>信标){
        postNotification(输入区域);
      }

      @覆盖
      公共无效onExitedRegion(地区区域){
        postNotification(已退出区域);
      }
    });
  }

  @覆盖
  公共布尔onOptionsItemSelected(菜单项项){
    如果(item.getItemId()== android.R.id.home){
      完();
      返回true;
    }
    返回super.onOptionsItemSelected(项目);
  }

  @覆盖
  保护无效onResume(){
    super.onResume();
    notificationManager.cancel(NOTIFICATION_ID);
    beaconManager.connect(新BeaconManager.ServiceReadyCallback(){
      @覆盖
      公共无效onServiceReady(){
        尝试 {
          beaconManager.startMonitoring(区);
        }赶上(RemoteException的E){
          Log.d(TAG,错误而启动的监控);
        }
      }
    });
  }

  @覆盖
  保护无效的onDestroy(){
    notificationManager.cancel(NOTIFICATION_ID);
    beaconManager.disconnect();
    super.onDestroy();
  }

  私人无效postNotification(弦乐味精){
    意图notifyIntent =新的意图(NotifyDemoActivity.this,NotifyDemoActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(
      NotifyDemoActivity.this,
      0,
      新的意图[] {} notifyIntent,
      PendingIntent.FLAG_UPDATE_CURRENT);
    通知通知=新Notification.Builder(NotifyDemoActivity.this)
    .setSmallIcon(R.drawable.beacon_gray)
    .setContentTitle(通知演示)
    .setContentText(MSG)
    .setAutoCancel(真)
    .setContentIntent(pendingIntent)
    .setVibrate(mVibratePattern)
    。建立();
    notification.defaults | = Notification.DEFAULT_SOUND;
    notification.defaults | = Notification.DEFAULT_LIGHTS;
    notificationManager.notify(NOTIFICATION_ID,通知);

    TextView的statusTextView =(TextView中)findViewById(R.id.status);
    statusTextView.setText(MSG);
  }
}
 

解决方案

您应该持有 BeaconManager 在你的应用程序类不在活动。

活动将被停止,销毁和 BeaconManager 将停止监视。在另一方面,应用程序仍然会持有参考,并会继续密切留意。

当信标被发现,同时监视您的应用程序类可以发布一个通知。这引发了一些活动,如果用户决定点击就可以了。

I would like to have push notifications when my app is open but is in background. For now I have changed the Estimote Demo, and my app gives me a notification when my app is in foreground which is not much of use. I post here my code of NotifyDemoActivity class which is called as soon as I open the app

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

解决方案

You should hold BeaconManager in your application class not in the activity.

Activity will be stopped, destroyed and BeaconManager will stop monitoring. Application on the other hand will still hold reference and will continue to monitor.

When a beacon is being found while monitoring your application class can post a notification. It trigger some activity when user decides to tap on it.

这篇关于Estimote iBeacon:在后台监视(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆