Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a button that when pressed, starts a service. This service starts logging information from the device. However, when I press the run, the screen temporarily freezes for about 8 seconds whilst it logs, and then once it is finished the screen unfreezes, a toast message shows (that's meant to show as soon as you press the button) and then I can do whatever. If i go on the application screen again, when the application is logging (note: the service is always running so that doesnt freeze it, just the logging) then the activity is frozen again and doesnt let me do anything until the logging is complete.

I have tried asynctasks and running on a new runnable/new thread but none of these have seemed to work for me. Whether i am implementing them correctly or not I'm unsure, but I'd like to fix the problem.

Here is my onStartCommand in the service class:

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // THIS WOULD BE THE METHOD THAT RUNS WHATEVER YOU WANT TO DO EVERY SO OFTEN SO FOR ME THIS IS GETTING ALL DATA ETC
    getProcessInfo();
    //  LOG DELAY = SECONDS BETWEEN RUNNING SERVICE/METHOD. SET AT THE TOP
    myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+LOG_DELAY*1000, myPendingIntent);
    Intent notificationIntent = new Intent(this, LogOrCheck.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this,
            101, notificationIntent,
            PendingIntent.FLAG_NO_CREATE);
    NotificationManager nm = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Resources res = this.getResources();
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.arrow_up_float)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.arrow_down_float))
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Androigenius")
                .setContentText("Service running OK");
    Notification n = builder.getNotification();
    startForeground(100, n);
    return Service.START_STICKY;

And here is where I call startService:

but1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (isClicked) {
                Context context = getApplicationContext();
                Toast toast = Toast.makeText(context, "Please press 'MINIMIZE' at the top to continue logging.", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP|Gravity.RIGHT, 10, 55);
                toast.show();
                System.out.println("Start logging");
                but1.setText("Stop Logging");
                but1.setTextColor(Color.RED);
                // START SERVICE, DONE. (STOP IS BELOW).
                startService(myIntent);
                isClicked = false;
            else if (!isClicked) {
                System.out.println("Stop Logging");
                but1.setText("Start Logging");
                // STOP SERVICE, DONE.
                stopService(myIntent);
                but1.setTextColor(getResources().getColor(color.cornblue));
                isClicked = true;
                I have the same issue..  Well,I understand that Intent service is running on a separate thread but my UI is still freezing up. I am doing a long db operation on the intent service , I dont understand why it is freezing up as it running on a completely different thread.
– Guru
                Sep 12, 2013 at 14:14

Services run on the UI thread which is causing the 'freeze'. Using an IntentService will create a service that automatically runs in the background, not on the UI thread. Here is some details of the IntentService class:

IntentService Class

By default, the Service also run in the main thread of your applicaiton, in which your UI operations occur. Therefore, if you perform a long task in your onStartCommand() method, it will block the main thread.

To avoid the problem, you have to migration the logging task into a separate thread. You may use AsycTask class to do so. Please refer to http://developer.android.com/reference/android/os/AsyncTask.html for how to use the class.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.