4

I have one service(BackgoundService), one intent service (MyIntentService) and one process in my android app. UI hangs i.e. freezes completely when all three are running but it becomes so responsive when I stop the Intent Service.

Also I am running the BackgroundService in a worker thread and documentation says that Intent Service runs in a separate worker thread by default.

I don't get why my ui thread is still getting blocked when all other operations ,services are running in separate threads.

Intent service

     public class MyIntentService extends IntentService {
    public static String ACTION ="com.example.mediaapp.prototypingcanvas.MyIntentService.DONE_Processing";

    public  MyIntentService(){
       super("MyIntentService");
    }
    DBRepo dbRepo;
    WebRepo webRepo;
    FileRepo fileRepo;
    @Override
    protected void onHandleIntent(Intent intent) {

        //Activity Types ; 1=uploadSurvey; 2=downloadQre; 3=download survey for review; 4=sample download; 5=projectlistdownload
        webRepo = new WebRepo(this);
        fileRepo = new FileRepo();
        int queueID = intent.getIntExtra("ID", 0);
        dbRepo = new DBRepo(getApplicationContext());
        dbRepo.updateQueue(queueID);
        List<String> filesList;
        int activityType = intent.getIntExtra("ActivityType", 0);
        // int projectID = intent.getIntExtra("ProjectID",0);
        String userName = intent.getStringExtra("UserName");
        String ProjectGUID = intent.getStringExtra("ProjectGUID");
        String projectName = intent.getStringExtra("ProjectName");

        int response = -1;
        Log.i("activitytype", activityType + "");


        if (activityType == 1) {
            filesList = new ArrayList<>();
            filesList = fileRepo.list(projectName);
            for (String fileName : filesList) {
                webRepo.SyncData("http://test.cloud.rebuscode.com/api/api/v2/respondent/SyncData/" + projectName + "/" + fileName + "/true");
            }
            Log.i("activity type", activityType + "");
            response = 1;
        } else if (activityType == 2) {
            Log.i("activity type", activityType + "");
            response = 0;
        } else if (activityType == 3) {
            Log.i("activity type", activityType + "");
            //webRepo.DownloadQuestionnaire("http://test.cloud.rebuscode.com/api/api/v2/respondent/DownloadQuestionnaireForTab/" + projectGuid + "/" + projectName,projectName);
            fileRepo.unzip("Splash/and.zip");
        } else if (activityType == 4) {
            Log.i("activity type", activityType + "");
            response = 1;
        } else if (activityType == 5) {
            Log.i("activity type", activityType + "");
            response = 0;
        } else {
            Log.i("activity type", activityType + "");
        }

        // processing done here….
        //delete row from Queue table and broadcast
        dbRepo.deleteQueue(queueID);
        Intent broadcastIntent = new Intent(ACTION);
        broadcastIntent.putExtra("Response", response);
        broadcastIntent.putExtra("ActivityType", activityType);
        broadcastIntent.putExtra("UserName", userName);
        broadcastIntent.putExtra("ID", queueID);
        broadcastIntent.putExtra("ProjectGuid", ProjectGUID);
        sendBroadcast(broadcastIntent);

    }
}

can anyone suggest why its blocking my ui thread???

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
Rojy Kumari
  • 159
  • 11
  • How you call your service? – Sree Dec 16 '15 at 08:43
  • Where and how are you handling the broadcast sent by the IntentService? – Mike M. Dec 16 '15 at 08:54
  • Even if you use Service, you will need Web api requests in Background thread only.. – Chintan Soni Dec 16 '15 at 08:55
  • @ChintanSoni An IntentService's `onHandleIntent()` method already runs on a background worker thread. – Mike M. Dec 16 '15 at 08:59
  • just create your own async task, do the network operation in `doInBackground()` and from `onPostExecute()` fire your broadcast.. thats it.. – Chintan Soni Dec 16 '15 at 09:00
  • 1
    thanku all for your suggestions and comment. I found the solution. Actually the problem was with my broadcast handling. It was running on main thread and making my app unresponsive, Handled it in diff thread and app is running smoothly :) – Rojy Kumari Dec 16 '15 at 09:10
  • @RojyKumari can you please explain how you handled broadcast in a different thread. Code snippet will help – nadafafif May 11 '17 at 19:16

0 Answers0