android – 如何为kontakt.io sdk实现后台服务?
作者:互联网
如果我使用Android设备进入信标区域,我想显示通知.这在Android SDK Quickstart有很好的记录
只要应用程序处于活动状态,这只是工作.应用程序关闭后如何收到通知?
解决方法:
我自己建立了一项服务:
BeaconRangingService.java
public class BeaconRangingService extends Service {
private static final String TAG = BeaconRangingService.class.getSimpleName();
private BeaconManager beaconManager;
@Override
public void onCreate() {
super.onCreate();
beaconManager = BeaconManager.newInstance(getApplicationContext());
beaconManager.setMonitorPeriod(MonitorPeriod.MINIMAL);
beaconManager.setForceScanConfiguration(ForceScanConfiguration.DEFAULT);
beaconManager.registerMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onMonitorStart() {
Log.v(TAG, "start monitoring beacons");
}
@Override
public void onMonitorStop() {
Log.wtf(TAG, "stop monitoring beacons");
}
@Override
public void onBeaconsUpdated(Region region, List<Beacon> list) {
}
@Override
public void onBeaconAppeared(Region region, Beacon beacon) {
Toast.makeText(getApplicationContext(), "Beacon appeared\n BEACON ID: " + beacon.getBeaconUniqueId(), Toast.LENGTH_SHORT).show();
}
@Override
public void onRegionEntered(Region region) {
}
@Override
public void onRegionAbandoned(Region region) {
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "service started");
if (!beaconManager.isBluetoothEnabled()) {
Log.w(TAG, "bluetooth disabled, stop service");
stopSelf();
} else {
connect();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Log.v(TAG, "service destroyed");
beaconManager.stopMonitoring();
beaconManager.disconnect();
beaconManager = null;
super.onDestroy();
}
private void connectBeaconManager() {
try {
beaconManager.connect(new OnServiceBoundListener() {
@Override
public void onServiceBound() {
try {
HashSet<Region> regions = new HashSet<>();
regions.add(Region.EVERYWHERE);
beaconManager.startMonitoring(regions);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
} catch (RemoteException e) {
throw new IllegalStateException(e);
}
}
}
AndroidManifest.xml中
<service
android:name="com.your.package.BeaconRangingService"
android:exported="false"/>
开始服务
Intent intent = new Intent(this, BeaconRangingService.class);
startService(intent);
标签:android,ibeacon,ibeacon-android,android-ibeacon 来源: https://codeday.me/bug/20190624/1275823.html