2016-04-05 30 views
0

和抱歉我的英語。 我正在與iBeacon合作,爲此,我正在使用Android Beacon Library。 這是偉大的,完美的工作,但現在我需要你的幫助。定製服務和AltBeacon

我有一個線程,當某個iBeacon進入該區域時啓動併發送信息,並在iBeacon離開該區域時停止。 問題是,當我殺了應用程序,線程死亡。 我想到一個服務,但我發現使用BootstrapNotifier是不可能使用另一個自定義服務。

那麼,你有關於如何完成這項任務的任何想法? 在此先感謝您的建議。

回答

1

好的,我解決了另一種方式。 我正在使用我的自定義服務,並且沒有在應用程序中實施更多的BootstrapNotifier。

這是我的代碼,如果有人需要它。

public class BeaconDetector extends Service implements BeaconConsumer { 

private static final String TAG = "BeaconDetector"; 

private BeaconUtility.BeaconObject beaconObject; 

private Context getServiceCtx(){ 
    return BeaconDetector.this; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    IMLog.e(TAG, "Created."); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    IMLog.e(TAG, "Start."); 

    beaconObject = BeaconUtility.instantiateBeaconManager(this); 
    beaconObject.beaconManager.bind(this); 

    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    IMLog.e(TAG, "Destroy."); 
    beaconObject.beaconManager.unbind(this); 
} 


@Override 
public void onBeaconServiceConnect() { 
    IMLog.e(TAG, "Connected."); 

    beaconObject.beaconManager.setMonitorNotifier(new MonitorNotifier() { 
     @Override 
     public void didEnterRegion(Region arg0) { 
      // In this example, this class sends a notification to the user whenever a Beacon 
      // matching a Region (defined above) are first seen. 
      IMLog.e(TAG, "did enter region."); 
      Sender.getInstance(getServiceCtx()).startSender(); 
     } 

     @Override 
     public void didExitRegion(Region region) { 
      IMLog.e(TAG, "did exit region."); 
      if (Sender.getInstance(getServiceCtx()).isAlive()) { 
       Sender.getInstance(getServiceCtx()).stopSender(); 
      } 
     } 

     @Override 
     public void didDetermineStateForRegion(int state, Region region) { 
      IMLog.e(TAG, "did enter region."); 
     } 
    }); 

    try { 
     beaconObject.beaconManager.startMonitoringBeaconsInRegion(BeaconUtility.getMonitoringRegion()); 
    } catch (RemoteException e) { 
     IMLog.e(TAG, "Remote Exception."); 
    } 

} 

}

+0

很高興你解決它!與使用RegionBootstrap相比,這是一個更好的方法,它實際上是設計用於'Application'類的。 – davidgyoung