【问题标题】:Why does my callstack say that I have 2 mains running?为什么我的调用堆栈说我有 2 个电源正在运行?
【发布时间】:2020-10-20 21:10:51
【问题描述】:

我开始为 FCM 实现后台消息,他们要求我制作自己的 Kotlin 应用程序。但自从我这样做后,我的调用堆栈中似乎有 2 个主要功能。

这怎么可能?如果我运行一个空项目,它的调用堆栈中有 0 个主电源?

我添加了 Application.kt

package HERE_I_HAVE_MY_PACKAGE_NAME

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

public class Application: FlutterApplication(), PluginRegistrantCallback {
  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this)
  }

  override fun registerWith(registry: PluginRegistry) {
    FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
  }
}

还有 FirebaseCloudMessagingPluginRegistrant.kt

package HERE_I_HAVE_MY_PACKAGE_NAME

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

class FirebaseCloudMessagingPluginRegistrant {
  companion object {
    fun registerWith(registry: PluginRegistry) {
      if (alreadyRegisteredWith(registry)) {
        return;
      }
      FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
      val key = FirebaseCloudMessagingPluginRegistrant::class.java.name
      if (registry.hasPlugin(key)) {
        return true
      }
      registry.registrarFor(key)
      return false
    }
  }
}

这是我的 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company_name.app_name">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name=".Application"
        android:label="App Name"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
        android:name="com.yalantis.ucrop.UCropActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id"/>  
    </application>
</manifest>

【问题讨论】:

    标签: android flutter kotlin dart firebase-cloud-messaging


    【解决方案1】:

    我不确定是什么导致main() 运行多次(在我的情况下为 3 次),但我找到了解决方法

    void main() {
      runApp(new MaterialApp(
        home: new BeforeRunning(),
      ));
    }
    
    class BeforeRunning extends StatefulWidget {
      BeforeRunning({Key key}) : super(key: key);
    
      @override
      _BeforeRunningState createState() => _BeforeRunningState();
    }
    
    class _BeforeRunningState extends State<BeforeRunning> {
      @override
      void initState() {
        super.initState();
        Timer(Duration(milliseconds: 10), () {
          Navigator.of(context).pushReplacement(new MaterialPageRoute(builder: (BuildContext context) => MyApp()));
        });
      }
    
      @override
      Widget build(BuildContext context) {    //You can add a splash screen here
        SystemChrome.setEnabledSystemUIOverlays([]);
        return Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          color: Colors.black,
        );
      }
    }
    
    class MyApp extends StatefulWidget {
      MyApp({Key key}) : super(key: key);
    
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      //initState() here
      //build() here
    }
    

    基本上,在BeforeRunning()initState() 中添加一个快速的Timer()(10 毫秒)和Navigator.of(context).pushReplacement( /*next screen*/),从那时起任何屏幕都只会构建一次。 (为我工作)

    main()BeforeRunning() 运行不止一次,但 Timer() 以某种方式停止了除一个之外的所有线程

    【讨论】:

    • 在我的情况下不起作用
    猜你喜欢
    • 2015-10-04
    • 2021-05-11
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2016-12-24
    • 2011-02-10
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多