Fur*_*kul 5 android android-service android-service-binding foreground-service
注意:此问题假设您知道将服务绑定到上下文。
生产中的每个人,甚至一些拥有 Android Oreo 或 Pie 设备的用户都意识到了这个问题。异常看起来像这样:
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Run Code Online (Sandbox Code Playgroud)
当你打电话时,Context.startForegroundService(Intent)你也必须Service.startForeground(int, Notification) 在 5 秒内打电话(即使你的手机可能甚至无法启动你的服务,但由于某种原因它成为你的错),否则你的应用程序将冻结或从 Android 框架抛出异常。经过进一步思考,我制定了一个解决方案。
由于Context.startForegroundService(Intent)不保证服务将在 5 秒内创建并且是异步操作,因此我认为事情是这样进行的:
// Starts service.
public static void startService(Context context)
{
// If the context is null, bail.
if (context == null)
return;
// This is the same as checking Build.VERSION.SDK_INT >= Build.VERSION_CODES_O
if (Utilities.ATLEAST_OREO)
{
Log.w(TAG, "startForegroundService");
// Create the service connection.
ServiceConnection connection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
// The binder of the service that
// returns the instance that is created.
MyService.LocalBinder binder = (MyService.LocalBinder) service;
// The getter method to acquire the service.
MyService myService = binder.getService();
// getServiceIntent(context) returns the relative service intent.
context.startForegroundService(getServiceIntent(context));
// This is the key: Without waiting Android Framework
// to call this method inside Service.onCreate(),
// immediately call here to post the notification.
// MyService.createNotification(context) is static
// for other purposes.
myService.startForeground(19982, MyService.createNotification(context));
// Release the connection to prevent leaks.
context.unbindService(this);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
}
};
// Try to bind the service.
try
{
context.bindService(getServiceIntent(context), connection, Context.BIND_AUTO_CREATE);
}
catch (RuntimeException ignored)
{
// This is probably a broadcast receiver context
// even though we are calling getApplicationContext().
// Just call startForegroundService instead
// since we cannot bind a service to a
// broadcast receiver context.
// The service also have to call startForeground in this case.
context.startForegroundService(getServiceIntent(context));
}
}
else
{
// Normal stuff below API 26.
Log.w(TAG, "startService");
context.startService(getServiceIntent(context));
}
}
Run Code Online (Sandbox Code Playgroud)
该函数是从启动服务的活动中调用的。有趣的是:它可以工作并且不会抛出异常。我已经在模拟器上进行了测试,我的设备也是 Oreo,我想确认这是防止此异常发生的最佳方法。
我的问题是:这是实际创建前台服务的好方法吗?有什么想法吗?谢谢您的意见。
| 归档时间: |
|
| 查看次数: |
1296 次 |
| 最近记录: |