Does an Android service lock the application even running in the background?

Asked

Viewed 202 times

2

Does an Android service lock the application even running in the background? Or is there something wrong with my application?

I have a database where the last table has 3,000 lines. With this I decided to insert in background using a service to give faster to the user at the time of insertion. But this service is locking my application, and only releases my application when Thread inserts all data into the database.

Does anyone know what it might be? Follow the code.

public class ServiceAndroid extends Service{

    Thread thread;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();       

    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


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

        Toast.makeText(getApplicationContext(), "iniciou o service", Toast.LENGTH_LONG).show();


        thread = new Thread(){
            public void run(){
                try {
                    //meu código
                } catch (ClassNotFoundException | IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }       
        };
        thread.start();

        return START_STICKY;
    }


}

method where I call the service:

// inicia o service
   public void startService(View view){

       Intent i = new Intent(getBaseContext(), ServiceAndroid.class);
       startService(i);
   }
  • The code seems to me to be correct. Perhaps you should give more details about what is in the part "//meu código".

  • I did not put p/ facilitate the view, but are just calls of the insert method, nothing too much with the 'cv.put("key", value);'

  • Are you stopping the service after Thread finishes the job? He stated in Manifest?

  • Yes I stated, I’m not stopping because I don’t need to, after he inserted my table there’s nothing to do. It is working, the problem is that it is locking my application until inserting everything.

  • Locking how? Post the part of the code where you do the start of the service.

  • updated the question

  • Try to explain the behavior of the application after the service has started. You say it "hangs", which means exactly that?

  • When the service is started if I press any button it is only triggered when the service finishes inserting the table and terminating its process, and as if it is not working in the background.

  • For what I’ve been searching, the service even running in the background affects the Thread UI, what I can do is put the Thread of the service to "sleep" every time the Thread UI needs to be used.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.