将手机升级到8.1开发人员预览后,我的后台服务不再正常启动.
在我长期运行的服务中,我实现了一个startForeground方法来启动正在进行的通知,该通知在create上调用.
@TargetApi(Build.VERSION_CODES.O)
private fun startForeground() {
// Safe call, handled by compat lib.
val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
val notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build()
startForeground(101, notification)
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main
Process: $PACKAGE_NAME, PID: 24704
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Run Code Online (Sandbox Code Playgroud)
服务通知的通道无效,显然我的旧通道DEFAULT_CHANNEL_ID不再适用于我假设的API 27.什么是合适的渠道?我试图浏览一下文档
service android background-service android-notifications kotlin
我正在尝试测试GeoFences的位置感知应用程序.我已经获得了多达11次连续成功,但通常会有数十次失败.重新启动设备没有帮助.卸载/重新安装没有帮助.禁用位置,重新启用位置无济于事.更改设备位置准确度设置没有帮助.
我已经在v2.3.4,v4.0.3,v4.4.2,v4.4.4和2 v5.0.1物理设备上进行了测试.其中一个Lollipop设备是具有蜂窝服务的设备.其余设备是SIMless的,仅用于测试.旁注:没有服务的设备很难通过MockLocation设置它们的位置,但是如果他们设法设置它们的位置,他们几乎从不注册围栏入口/出口.
位置正在通过Mock Location更新,但GeoFence不会被任何可靠性触发.
我用谷歌搜索了它.我查看了developer.android.com网站上的文档.我搜索过StackOverflow.事实上,其他人提出的许多建议已在下面的代码中实现.
那么,我如何使用MockLocation可靠地触发我的GeoFence?
import android.location.Location;
import android.location.LocationManager;
public class GeoFenceTester extends ActivityInstrumentationTestCase2<GeoFenceTesterActivity> {
public static final String TAG = GeoFenceTester.class.getSimpleName();
private LocationManager locationManager;
private Activity activityUnderTest;
protected void setUp() throws Exception {
super.setUp();
activityUnderTest = getActivity();
/*
MOCK Locations are required for these tests. If the user has not enabled MOCK Locations
on the device or emulator these tests cannot work.
*/
if (Settings.Secure.getInt(activityUnderTest.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 0) {
throw new RuntimeException("Mock locations are currently disabled …Run Code Online (Sandbox Code Playgroud) android unit-testing geolocation android-location geofencing
我有一个地理围栏应用程序,我正在尝试移植它以在 Android 8+ 上工作。我已阅读有关该主题的教程,并切换到使用compile 'com.google.android.gms:play-services-location:16.0.0'.
当应用程序不在前台时,地理围栏输入事件永远不会触发。 我等了超过文档说可能需要的两分钟。我已经等了 15 分钟,但没有结果。只要我将应用程序带到地理围栏内的前台,它就会立即触发。
我知道在 Android 8+ 上对后台服务有限制,但 Google 的教程说使用 IntentService 应该在 Android 8+ 上被阻止。 事后编辑:上述教程是完全错误的。不要跟着它。IntentService 将不起作用。
我尝试按照这个答案,它说使用新的GeofencingClient来设置你的意图来解决这个问题。(我不明白为什么这会有所帮助,因为它仍然使用 IntentService 来接收事件,并且在我的代码中不起作用。)
This answer to a related question here表明您必须使用BroadcastReceiver而不是IntentService,但我不明白为什么这会有所作为,因为Android 8在后台对BroadcastReceiver施加了与IntentServices相同的限制。 事后编辑:这个链接的答案也是正确的。虽然隐式广播接收器受到限制,但在运行时注册以将请求发送到特定包的显式广播接收器不受限制并且可以正常工作。
是否真的有可能在 Android 8+ 上获得在后台唤醒您的应用程序的地理围栏回调?如果是这样,你怎么能做到这一点?
我的地理围栏设置:
googleApiClient = new GoogleApiClient.Builder(this.context)
.addApi(LocationServices.API)
.addConnectionCallbacks(getServiceSetup())
.addOnConnectionFailedListener(getServiceFailureStrategy())
.build();
geofencingClient = LocationServices.getGeofencingClient(context);
Intent intent = new Intent(context, mIntentService);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// …Run Code Online (Sandbox Code Playgroud) android location geofencing android-geofence android-8.0-oreo