Services: Unable to start service Intent

Hi
I wanted to run a piece of code in a background service so today I tried to test Services in AI2.
(this extension will be open source and free)
Here is the code:

//Start method

       try {
            context.startService(new Intent(context,BgService.class)
            .putExtra("PROCEDURE_NAME",procedureName));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context,e.getMessage(),Toast.LENGTH_LONG);
        }

//BgService class

public class BgService extends Service {
        public Context mContext;
        public NotificationManagerCompat notificationManager;
        public BgService(){
            super();
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            mContext = getApplicationContext();
            notificationManager = NotificationManagerCompat.from(mContext);
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,mContext.getApplicationInfo().name);
            //builder.setContentTitle(title);
            builder.setContentText("Service started by user");
            builder.setAutoCancel(false);
            builder.setVisibility(1);
            builder.setPriority(1);
            notificationManager.notify(12345,builder.build());
            //Toast.makeText(context, "Service started by user.", Toast.LENGTH_LONG).show();
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public void onDestroy() {
            //Toast.makeText(context, "Service stopped by user.", Toast.LENGTH_LONG).show();
            //notificationManager.cancel(12345);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,mContext.getApplicationInfo().name);
            //builder.setContentTitle(title);
            builder.setContentText("Service stopped by user");
            builder.setAutoCancel(false);
            builder.setVisibility(1);
            builder.setPriority(1);
            notificationManager.notify(12345,builder.build());
            super.onDestroy();
        }
    }

//Uses Services annotation

@UsesServices(services = {@ServiceElement(exported = "false",name = "com.sunny.BackgroundTask.BackgoundTask$BgService")})

I have also tried with 1) a period, and 2)without package

Here is the error I am getting:

Unable to start service Intent { cmp=appinventor.ai_vknow360.CustomWebView/com.sunny.BackgroundTask.BackgroundTask$BgService } U=0: not found

Thank you :slight_smile:

3 Likes

Kindly have a look here Sir @ewpatton

probably because an r is missing there?
Unbenannt

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

3 Likes

Thank you.
However I am sure that it should always work with period i.e. .BgService but actually it does not work.

My guess is that it's failing because your Service subclass is nested (based on the $ in the name) but the class isn't static. This implies that there is a constructor that takes the outer class as an implicit this, which means that Android can't construct it (the Service constructor must be nullary).

3 Likes

Thank you @Taifun and @ewpatton
Now it works fine after:-

  1. adding r to class name
  2. making the class static
2 Likes