Android没有从后台检测信标

时间:2015-09-18 07:38:48

标签: android ibeacon ibeacon-android altbeacon beacon

我正在使用Altbeacon库进行信标处理。我试图在背景中检测到信标,如here所述。我的应用活动会监控信标并正确返回值。但是当我杀死应用程序时,应用程序没有检测到信标。以下是我的Application类。

public class BeaconApplication extends Application implements BootstrapNotifier{

private static final String TAG = "BeaconApplication";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private MainActivity mainActivity = null;

@Override
public void onCreate() {
    super.onCreate();
    BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.setBackgroundScanPeriod(5000l); // 5 secs
    beaconManager.setForegroundScanPeriod(5000l); // 5 secs
    Log.d(TAG, "setting up background monitoring for beacons and power saving");
    Region region = new Region("backgroundRegion",
            Identifier.parse("fda50693-a4e2-4fb1-afcf-c6eb07647825"), Identifier.parse("10004"), Identifier.parse("54480"));
    regionBootstrap = new RegionBootstrap(this, region);

}

@Override
public void didEnterRegion(Region region) {
    Log.d(TAG, "did enter region. UniqueId: "+ region.getId1());
    if (!haveDetectedBeaconsSinceBoot) {
        Log.d(TAG, "auto launching MainActivity");
        sendNotification();

        haveDetectedBeaconsSinceBoot = true;
    } else {
        if (mainActivity != null) {
            mainActivity.logToDisplay("I see a beacon again" );
        } else {
            Log.d(TAG, "Sending notification.");
            sendNotification();
        }


 }

}



@Override
public void didExitRegion(Region region) {
    if (mainActivity != null) {
        mainActivity.logToDisplay("I no longer see a beacon.");
    }
}

@Override
public void didDetermineStateForRegion(int i, Region region) {
    if (mainActivity != null) {
        mainActivity.logToDisplay("I have just switched from seeing/not seeing beacons: " + i);
    }
}

private void sendNotification() {
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setContentTitle("Welcome to beacon zone")
                    .setContentText("You are in a beacon zone.")
                    .setSmallIcon(R.mipmap.ic_launcher);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    builder.setContentIntent(resultPendingIntent);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    builder.setSound(alarmSound);
    builder.setAutoCancel(true);
    Notification notification = builder.build();
    notification.flags = Notification.DEFAULT_LIGHTS;
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
    NotificationManager notificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

public void setMonitoringActivity(MainActivity activity) {
    this.mainActivity = activity;
}
}

这是我的清单文件

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:name=".BeaconApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:launchMode="singleInstance" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

1 个答案:

答案 0 :(得分:0)

有些用户报告某些三星设备禁止应用功能,您可以从任务切换器中删除该应用,直到手动重新启动。标准的Android行为是从任务切换器中杀死应用程序允许自动重新启动,但在设置中使用强制停止杀死应用程序不允许自动重新启动。似乎在某些三星版本中,使用任务切换器滑动会执行非标准强制停止,而不是Android规定的常规停止操作。

Android Beacon Library中有一个tracking issue。 In包含指向special test app的链接,您可以构建并使用该链接来表征此功能在您的设备上的工作方式。按照本期中的说明并报告结果将非常有助于弄清楚发生了什么。无论好坏,Android的碎片性质意味着每个设备的行为略有不同,必须单独表征。

除了运行上述内容之外,了解具有此问题的特定设备上的操作系统版本和内部版本号会很有帮助。