java.lang.ClassNotFoundException: Didn't find class , Foreground service could not start

Vuyiswa Maseko 351 Reputation points
2022-02-14T07:17:34.197+00:00

Good Day All

i have a Foreground service , in some Huawei and other Android phones on

Android 10,

i get the following error

java.lang.ClassNotFoundException: Didn't find class "com.company.appname.startServiceAndroid" on path: DexPathList[[zip file "/data/app/com.company.appname-fsvJ_7eH53yglcn8Ju9x5Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.company.appname-fsvJ_7eH53yglcn8Ju9x5Q==/lib/arm64, /data/app/com.company.appname-fsvJ_7eH53yglcn8Ju9x5Q==/base.apk!/lib/arm64-v8a, /system/lib64, /hw_product/lib64, /system/product/lib64]]

and my service is defined like this

[

assembly: Dependency(typeof(startServiceAndroid))]
namespace Myapp.Droid
{
    [Service]
    class startServiceAndroid : Service, IStartService
    { 
        public void StartForegroundServiceCompat()
        {
            var intent = new Intent(this, typeof(startServiceAndroid));

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
              StartForegroundService(intent); 
            }
            else
            {
              this.Application.ApplicationContext.StartService(intent); 
            }
        }

        public override IBinder OnBind(Intent intent)
        {
            return null;
        }

        public override void OnCreate()
        {
            base.OnCreate();
        }

        public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            try
            {
                // From shared code or in your PCL
                CreateNotificationChannel();

                string messageBody = "Push Notification Service";
                if (Build.VERSION.SdkInt > BuildVersionCodes.O)
                {
                    var notification = new Notification.Builder(this, "10111")
                .SetContentTitle("Myapp")
                .SetContentText(messageBody)
                .SetSmallIcon(Resource.Drawable.onesignal)
                .SetOngoing(true)
                .Build();

                 StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification); 

                     new BackgroundStartup();
                }
                else
                { 
                    ApplicationContext.StartService(intent); 

                }

            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }

            return StartCommandResult.Sticky;
        }
        void CreateNotificationChannel()
        {
            var channelName = "Myapp";
            var channelDescription = "Push Notification for Myapp";
            string NOTIFICATION_CHANNEL_ID = "10111";

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            { 

                var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
                {
                    Description = channelDescription
                };
                var notificationManager = (NotificationManager)GetSystemService(NotificationService);
                notificationManager.CreateNotificationChannel(channel); 
            }
            else
            {
                var notificationManager = (NotificationManager)GetSystemService(NotificationService);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
               .SetDefaults((int)NotificationDefaults.All)
               .SetSmallIcon(Resource.Drawable.onesignal)
               .SetVibrate(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 })
               .SetSound(null)
               .SetPriority(NotificationCompat.PriorityDefault)
               .SetAutoCancel(false)
               .SetContentTitle(channelName)
               .SetContentText(channelDescription)
               .SetOngoing(true);
                notificationManager.Notify(10111, notificationBuilder.Build());
            }

        }


        public override void OnDestroy()
        {  
            var notificationManager = (NotificationManager)GetSystemService(NotificationService); 
            base.OnDestroy();
        }
    }
}

and my oncreate on the main activity

protected override void OnCreate(Bundle savedInstanceState)
{

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            Handler h = new Handler();
            Action myAction = () =>
            {
                // your code that you want to delay here
                Application.ApplicationContext.StartForegroundService(intentservice);
            };
            h.PostDelayed(myAction, 1000);

        }
        else
        {


            Handler h = new Handler();
            Action myAction = () =>
            {
                // your code that you want to delay here
                this.StartService(intentservice);
            };
            h.PostDelayed(myAction, 1000);

        }


}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,334 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.