0

I have just created simple services on clicking of buttons and start the service inside AsynTask class still but getting message on logcat I/Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread. WHY ?

Here is my code

MainActivity.java

package com.example.servicesandroidtutu;
import androidx.appcompat.app.AppCompatActivity;    
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private MyAsynTask myAsynTask;
    private Button start, stop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start = findViewById(R.id.button);
        stop = findViewById(R.id.button2);

        myAsynTask = new MyAsynTask();

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myAsynTask.execute();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myAsynTask.cancel(true);
            }
        });

    }

    class MyAsynTask extends AsyncTask<Void,  Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            startService(new Intent(getApplicationContext(), MyService.class));
        }

        @Override
        protected Void doInBackground(Void... voids) {

            return null;
        }

        @Override
        protected void onCancelled(Void aVoid) {
            super.onCancelled(aVoid);
            stopService(new Intent(getApplicationContext(), MyService.class));
        }
    }

}

MyService.java

   package com.example.servicesandroidtutu;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyService extends Service {


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

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("Service", "stopped...");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d("Service", "started...");

        return super.onStartCommand(intent, flags, startId);
    }
}
  • Does this answer your question? https://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread. If not [EDIT](https://stackoverflow.com/posts/64190825/edit) the question to add the (properly formatted) stacktrace. – Stephen C Oct 04 '20 at 03:34
  • You cannot cancel your asynctask in this way as it has long finished when you try to do so. So you cannot stop the service either. – blackapps Oct 04 '20 at 09:27

1 Answers1

0

Your code is making heavy processing and it takes time to process and these skipped 31 frames happen because of it. What is your service doing? Do not perform any heave operation on the main thread. For more details check out this page

Also, I would suggest you not using AsyncTask, because it's has been deprecated since API 30.

AsyncTask deprecation

Google officially recommends using Kotlin coroutines or java threads.

  • In my service i am just log the message. i have used Thread also but still getting same – Hemant Sood Oct 04 '20 at 03:57
  • I also noted, you have not canceled AsyncTask in onDestroy(), when your activity is destroying. @Override protected void onDestroy() { if (myAsynTask != null) { myAsynTask.cancel(true); } super.onDestroy(); } – Maryna Meier Oct 04 '20 at 04:01
  • Instead i have used onCancelled() which will destroy the asyntask as well as stopService – Hemant Sood Oct 04 '20 at 04:07
  • When do you call onCancelled? You should not call it inside a button click listener to avoid a memory leak – Maryna Meier Oct 04 '20 at 13:08