如果使用Android后台服务,则Flutter会停留在“等待天文台端口可用” [英] Flutter stuck at 'waiting for observatory port to be available' if Android background services is used

查看:303
本文介绍了如果使用Android后台服务,则Flutter会停留在“等待天文台端口可用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为Flutter开发平台代码,以启动后台服务。在这里,我使用了一个最小的示例,其中没有完成 actual 的工作,以表明该应用程序根本无法运行。实际的颤动代码根本不会被修改。

I been trying to get write platform code for Flutter to get a background service started. In here, I used a minimal example with no actual work done to show that the application simply won't run. The actual flutter code is not modified at all.

MainActivity.java

public class MainActivity extends FlutterActivity {

  Intent i = new Intent(this, MainService.class);

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

MainService.java

public class MainService extends IntentService {
  public MainService() {
    super("MainService");
  }

  @Override
  protected void onHandleIntent(Intent Intent) {
  }
}

AndroidManifest.xml

 <service android:enabled="true"
android:name=".MainService"></service>

buildVersion为> 27,清单文件具有服务

The buildVersion is >27 and Manifest file has the service tag added accordingly.

使用 flutter run -v 进行编译和运行将显示以下消息:

Compiling and running with flutter run -v will show the following message:

..
[ +121 ms] Starting: Intent { act=android.intent.action.RUN flg=0x20000000 cmp=com.example.hello/.MainActivity (has extras)}
[   +1 ms] Waiting for observatory port to be available...

安装卡住了。

是否有解决方法?如果这是Flutter实现中的实际错误,是否还意味着没有在后台运行Flutter应用程序的方法?

Is there a workaround about this? If this is an actual bug in Flutter's implementation, does it also mean there is no way to run a Flutter application in the background?

推荐答案

我遇到了这个问题,并最终找到了我在AndroidManifest中将项目的程序包名称更改为com.test。 MyAppName 的事实。这不是有效的Dart软件包名称。您可以通过尝试使用

I had this problem and eventually tracked it down to the fact that I'd changed the package name of my project to com.test.MyAppName in the AndroidManifest. This is not a valid Dart package name. You can verify this by trying to create a new project using

flutter create --org com.test MyAppName

这将导致错误消息:


MyAppName无效Dart软件包名称。

"MyAppName" is not a valid Dart package name.

来自公共格式
说明

DO 使用 lowercase_with_underscores

DO use lowercase_with_underscores for package names.

软件包名称应全部小写,并用下划线分隔
个字, just_like_this 。仅使用基本的拉丁字母和阿拉伯
数字:[a-z0-9_]。另外,请确保该名称是有效的Dart标识符
-它不是以数字开头并且不是保留字。

Package names should be all lowercase, with underscores to separate words, just_like_this. Use only basic Latin letters and Arabic digits: [a-z0-9_]. Also, make sure the name is a valid Dart identifier -- that it doesn't start with digits and isn't a reserved word.

对我来说,解决方案是通过运行

The solution for me was to recreate my project with the desired values by running

flutter create --org com.test重新创建具有所需值的项目。 my_app_name

并跨lib文件夹,pubspec文件,任何资产等进行复制。这应该可以解决问题。请注意,这会导致kotlin文件夹(android / app / main / kotlin)具有根据您指定的域正确设置的子文件夹,例如kotlin / com / test / com.test

and copying across the lib folder, pubspec file, any assets, etc. This should fix the problem. Note that this results in the kotlin folder (android/app/main/kotlin) having subfolders set up correctly based on the domain that you specified, e.g. kotlin/com/test/ for com.test

这篇关于如果使用Android后台服务,则Flutter会停留在“等待天文台端口可用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆