本文介绍了我如何实施kontakt.io SDK后台服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我进入一个灯塔的区域与我的Android设备,我喜欢表现出通知。那是有据可查的,在的Android SDK快速
If I enter a region of a beacon with my Android device I like to show a notification. Thats well documented at Android SDK Quickstart
这是刚参加工作,只要应用程序是积极的。 如何做,我得到通知,当应用程序被关闭?
This is just working as long as the app is active. How do I get notifications when the app is closed?
推荐答案
我通过自己搭建服务:
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);
这篇关于我如何实施kontakt.io SDK后台服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!