0
我正在使用Alt信標庫進行信標掃描。我正在掃描信標服務類。我想繼續掃描信標,即使應用程序關閉也。但如果關閉應用程序服務被採空和信標不scanning.I試過,但我不getting.please幫助我,感謝
// MainActivity如何從服務發送數據,甚至關閉應用程序
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this, ScaningService.class);
startService(intent);//starting service
getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);//bounding service
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
//服務類
public class ScaningService extends Service implements BeaconConsumer {
public BeaconManager beaconManager;
public MainActivity activity;
private final IBinder mBinder = new SimpleServiceBinder();
public class SimpleServiceBinder extends Binder {
public ScaningService getService() {
return ScaningService.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
beaconManager = BeaconManager.getInstanceForApplication(getBaseContext());
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.setBackgroundMode(true);
beaconManager.setBackgroundScanPeriod(1100);//this will set how long a bluetooth should scan
beaconManager.setBackgroundBetweenScanPeriod(2000);//this will set bluetooth scanning interval
beaconManager.bind(ScaningService.this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
beacon = beacons.iterator().next();
Log.e(TAG, " UUID: " + beacon.getId1());// this is not showing and i tried toast also
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("sBeacon", null, null, null));
} catch (RemoteException e) {
Log.i(TAG, "RemoteException: " + e);
}
}
}
使用背景'服務' –
[圖書館的文件](https://altbeacon.github.io/android-beacon-library/documentation.html)也談到後臺操作。 –
請[編輯]你的問題,以顯示[你到目前爲止嘗試過的](http://whathaveyoutried.com)。您應該包含您遇到問題的代碼[mcve],然後我們可以嘗試幫助解決特定問題。你還應該閱讀[問]。 –