如何在应用程序关闭时在android作业服务中初始化mvvmcross表单IoC以使用注册服务?

我的 mvvmcross android 表单应用程序中有工作服务,该应用程序正在检查来自服务器的数据,如果是新帖子,则创建新通知。检查新帖子的代码正在使用中,所以我必须初始化 mvvmcross IoC,但那时我遇到了错误。


 [Service(Exported = true, Permission = "android.permission.BIND_JOB_SERVICE")]

    public class NotificationJobService : JobService

        {

            private static readonly string TAG = "ExampleJobService";


            public override bool OnStartJob(JobParameters args)

            {

                Log.Info(TAG, "on start job: " + args.JobId);


                DoBackgroundWork(args);


                return true;

            }


            public override bool OnStopJob(JobParameters args)

            {

                return true;

            }


            private void DoBackgroundWork(JobParameters args)

            {

                new Thread(() =>

                {

                    var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);

                    setupSingleton.EnsureInitialized();


                    Mvx.IoCProvider.Resolve<INotificationService>().ShowNewPostNotification(new Newsfeed());


                    JobFinished(args, true);


                }).Start();

            }     

        }



    [Activity(Label = "SurrenderAt20", MainLauncher = true, Theme = "@style/MainTheme", NoHistory = false, ScreenOrientation = ScreenOrientation.Portrait)]

    public class MainActivity : MvxFormsAppCompatActivity<AndroidSetup, CoreApp, App>

    {


    protected override void OnCreate(Bundle bundle)

    {        

        base.OnCreate(bundle);


        StartJob();

    }


    void StartJob()

    {

        Class javaClass = Class.FromType(typeof(NotificationJobService));

        ComponentName componentName = new ComponentName(this, javaClass);


        JobInfo info = new JobInfo.Builder(123, componentName)

           .SetMinimumLatency(20000)

           .SetOverrideDeadline(25000)

           .SetPersisted(true)

           .Build();

    

    }

}


叮当猫咪
浏览 115回答 1
1回答

呼如林

问题最初在评论中回答并作为答案发布,希望对其他人有所帮助。按照惯例,您的安装类必须命名为“Setup”,因此您需要重命名AndroidSetup为Setup背景:看Mvvmcross源码,初始化逻辑使用反射和约定来初始化自己。它正在寻找一个名为“Setup”的类。在您的情况下, MvvmCross 找不到您的安装程序类并抛出了您在堆栈跟踪中读取的异常MvvmCross.Exceptions.MvxException: Could not find a Setup class for application
打开App,查看更多内容
随时随地看视频慕课网APP