gpt4 book ai didi

android - 如何为 kontakt.io sdk 实现后台服务?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:37 25 4
gpt4 key购买 nike

如果我用我的 Android 设备进入信标区域,我想显示一条通知。这在 Android SDK Quickstart 中有详细记录

只要应用程序处于 Activity 状态,它就可以正常工作。 如何在应用关闭时收到通知?

最佳答案

我自己构建一个服务:

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 - 如何为 kontakt.io sdk 实现后台服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28088870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com