Altbeacon:禁用前景模式并从Notifcation AddAction终止应用程序

时间:2018-11-13 15:44:59

标签: android beacon altbeacon

我最近正在尝试构建一个使用AltBeacon库的应用程序。

我现在面临的问题是:我以这种方式创建了一个Application类:

    beaconManager = beaconManager.getInstanceForApplication(this);

    beaconManager.getBeaconParsers().clear();
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));


    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.setRegionStatePersistenceEnabled(false);

    Notification.Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.icon);



    builder.setBadgeIconType(Notification.BADGE_ICON_NONE);


    Intent buttonIntent = new Intent(getApplicationContext(), ButtonReceiver.class);
    buttonIntent.putExtra("notificationId",NOTIFICATION_ID);

    PendingIntent btPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, buttonIntent,0);


    builder.addAction(R.drawable.stop,"Termina Rilevamento.", btPendingIntent);



    builder.setColor(getResources().getColor(R.color.mygreen));

    builder.setContentIntent(btPendingIntent);

    builder.setPriority(Notification.PRIORITY_MAX);


    builder.setContentTitle("Rilevazione autenticazione in corso.");
    Intent intent = new Intent(this, LoginActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
    );
    builder.setContentIntent(pendingIntent);



    NotificationChannel channel = new NotificationChannel("My Notification Channel ID",
                "My Notification Name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("My Notification Channel Description");
    NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
    builder.setChannelId(channel.getId());



    beaconManager.enableForegroundServiceScanning(builder.build(), 456);


    beaconManager.setEnableScheduledScanJobs(false);
    beaconManager.setBackgroundBetweenScanPeriod(0);
    beaconManager.setBackgroundScanPeriod(1100);

    Region region = new Region("Region iniziale",null,Identifier.parse("789"),null);
    regionBootstrap = new RegionBootstrap(this, region);

    backgroundPowerSaver = new BackgroundPowerSaver(this);

通知上的Button指向BroadCastReceiver类的即ButtonReceiver的位置。 在按钮接收器中,我要执行禁用前台服务并关闭应用程序的操作。 我是这样做的:

    int notificationId = intent.getIntExtra("notificationId", 0);

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(notificationId);

    AndroidWalkEnrollmentApplication prova = new AndroidWalkEnrollmentApplication();
    prova.disableMonitoring();
在onReceive()方法中

所以问题是这样的:在disableMonitoring()中,我遵循Android-Beacon-Reference的示例,因此要禁用前景扫描,我这样做: bootstrap.disable()和bootstrap = null。问题在于它似乎不起作用。当我在前台模式的通知中按下按钮时,便会禁用笔记。我也尝试执行Beaconmanager.disableForegroundService(),但实际上并没有禁用它。

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

该库的2.15.2版本中存在一个错误,该错误会阻止正常停止前台服务。但是,好消息是:此实验性版本最近已修复了该问题:

https://github.com/AltBeacon/android-beacon-library/releases/tag/more-reliable-service-stop2

您可以按照发行版上的集成说明立即使用上述修补程序。此更改还将包含在1-2周的正式版本中。