1

我正在使用Altbeacon库开发信标。我正在尝试在后台检测信标,如此处所述。我的应用程序活动监控信标并正确返回值。但是当我杀死该应用程序时,该应用程序没有检测到信标。以下是我的应用程序类。

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>
4

1 回答 1

0

一些用户报告说,某些三星设备会禁止应用程序功能,如果您从任务切换器中终止应用程序,直到手动重新启动它。标准的 Android 行为是从任务切换器中终止应用程序允许自动重新启动,但在设置中强制停止终止应用程序不允许自动重新启动。似乎在某些三星版本中,使用任务切换器轻扫会执行非标准的强制停止,而不是 Android 规定的常规停止操作。

Android 信标库中有一个关于此的跟踪问题。其中包含一个指向特殊测试应用程序的链接,您可以构建和使用该应用程序来描述此功能在您的设备上的工作方式。遵循本期中的说明并报告结果对于弄清楚发生了什么非常有帮助。无论好坏,Android 的碎片化特性意味着每台设备的行为都略有不同,并且必须分别进行表征。

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

于 2015-09-21T16:04:07.487 回答