2017-02-13 11 views
1

나는이 스레드의 반대 문제가있는 문제로 실행하고 있습니다 :altbeacon이 백그라운드 서비스를 활성 상태로 유지하는 방법은 무엇입니까?

AltBeacon not detect beacon when app is closed

나는 altbeacon이 (http://altbeacon.org/)

앱이 인터페이스를 구현하는 altbeacon를 초기화 사용하는 응용 프로그램이 (세부 생략)

public class MyApp extends Application implements 
    BootstrapNotifier, 
    BeaconConsumer { 
//some code 
@Override 
public void onCreate() { 
    super.onCreate(); 
    initBeacons(); 
} 
public void initBeacons() { 
    mBackgroundPowerSaver = new BackgroundPowerSaver(this); 
    org.altbeacon.beacon.BeaconManager altBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this); 
    altBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); 
    // estimote 
    altBeaconManager.setBackgroundScanPeriod(5000); 
    altBeaconManager.setBackgroundBetweenScanPeriod(25000); 
    mBeaconManager = MyBeaconManager.getInstance(this, altBeaconManager); 
    mRegionBootstrap = new RegionBootstrap(this, MyBeaconManager.getRegions()); 
    altBeaconManager.bind(this); 
} 
@Override 
public void onBeaconServiceConnect() { 
    Thread thread = new Thread() { 
     public void run() { 
     // Try range the beacons 
     rangeMyBeacons(); 
     } 
    }; 
    thread.start(); 
} 
@Override 
public void didEnterRegion(Region region) { 
    // Some code 
} 
@Override 
public void didExitRegion(Region region) { 
    // Some code 
} 
@Override 
public void didDetermineStateForRegion(int i, Region region) { 
    // Some code  
} 
public class MyBeaconManager implements 
    RangeNotifier { 
// some code 

그러나 특수한 방송이나 앱없이 앱을 종료하거나 휴대 전화를 다시 시작한 경우 사용 권한 alt-beacon 서비스가 다시 살아납니다. Alt-beacon은 항상 다시 시작됩니다. 전경 서비스가 실행되지 않습니다. 다음은 모든 앱을 닫은 채 몇 시간 후 앱의 스크린 샷입니다 (그리고 휴대 전화를 재부팅했습니다). alt-beacon이 살아 있고 비컨을 스캔하는 것을 볼 수 있습니다. 나는 고도 - 비콘 코드를 볼 때

enter image description here 아직, 내가 도서관 내부 검색을 한 전경 서비스

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    LogManager.i(TAG, 
      intent == null ? 
        "starting with null intent" 
        : 
        "starting with intent " + intent.toString() 
    ); 
    return super.onStartCommand(intent, flags, startId); 
} 

하지 내가 START_REDELIVER_INTENT 또는 START_STICKY하는 안타를하지 참조하십시오.

제 질문은 앱이 죽을 때 alt-beacon이 서비스를 어떻게 유지합니까?

이 기능을 이해하려는 이유는 비슷한 서비스를 작성하고 있지만 포 그라운드 서비스로 포장하면 alt-beacon으로 작동한다는 것입니다. 내가 시도한 다른 모든 것은 앱이 닫히 자마자 서비스가 종료됩니다.

감사합니다.

답변

0

Android Beacon Library는 AlarmManager을 사용하여 백그라운드에서 검색 서비스를 계속 실행합니다. 주기적으로 알람을 5 분 동안 설정하면 운영 체제에서이 메시지를 전달하여 BroadcastIntent으로 지정하면 검색 서비스를 중지합니다. 실행 중일 때 라이브러리는 지속적으로이 경보를 다시 스케줄합니다.

https://github.com/AltBeacon/android-beacon-library/blob/master/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java#L339

// In case we go into deep sleep, we will set up a wakeup alarm when in the background to kickoff 
// off the scan cycle again 
protected void setWakeUpAlarm() { 
    // wake up time will be the maximum of 5 minutes, the scan period, the between scan period 
    long milliseconds = 1000l * 60 * 5; /* five minutes */ 
    if (milliseconds < mBetweenScanPeriod) { 
     milliseconds = mBetweenScanPeriod; 
    } 
    if (milliseconds < mScanPeriod) { 
     milliseconds = mScanPeriod; 
    } 

    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + milliseconds, getWakeUpOperation()); 
    LogManager.d(TAG, "Set a wakeup alarm to go off in %s ms: %s", milliseconds, getWakeUpOperation()); 
} 

이 디자인은 응용 프로그램이 메모리 부족으로 인해 종료 할 필요가있는 경우, 검사가에서 5 분 다시 시작됩니다 보장 :

당신은 여기에 수행하는 코드를 볼 수 있습니다 미래. 이것은 적당한 시간 프레임에서 비콘 탐지를 허용하면서 전화 집중적 인 작업을 완료하기 위해 전화 시간을 허용합니다.