Always use a separate thread for service and do not use the main GUI thread . If you use the main GUI thread for service, it may cause the the Application Not …

Nguồn:https://u-os.org/

Author

30 Comments

  1. José Vera Reply

    Great tutorial, I have a question, please help me: We only can create one service at a time?

  2. Gürkan Luş Reply

    if we use seperate thread , whats the meaning of using service

  3. Junaid Younas Reply

    why we make the synchronized block i see this keyword first time what is use of this???

  4. Thank you very much Prabeesh for creating this helpful tutorial….learning a lot from your videos.

  5. Exequiel Tiglao Reply

    How can I run speech recognition in background service? Can you help me please?

  6. sreepriya nousapp Reply

    hlo sir,great tutorial.can you create a video for push notification in a webview service?? thanks in advance…

  7. Make a video on service which gives toast for every 10sec even when the app is closed

  8. suneel kumar Reply

    hi sir,
    I was having a task like, where i have to insert/update A LOT OF DATA like 1000 rows with 80 colums each in the sqlite local DB, for that i was creating a thread.. in that thread i was creating a method like dbLogic(), the thing is after completion of the task i.e; insertion/updation completion i have to show an alert dialog, that alert dialog code i have written normally not in the thread..In some times the main problem causing is, the alert dialog is coming before only unless my another thread doesn't completed its work, i was trying to store 1000 data, in some times it was storing 200 data and the alert was coming,in some times it was storing 400 data alert was coming,in sometimes it was storing 550 or 600 or 650 the alert box was coming.. what i need is after successfully completion of the task only my main thread should execute. i heard that thread is an independent process… what to do please help me..

  9. anika anik Reply

    I have a switch button in my android application.
    I make a thread which work is randomly flashing. I put a Start method
    when switch button is on and interrupt the method when the button is of.
    It works the first time but the second time when I touch the switch
    button the application ends automatically. I press the button then the
    random flashing work then I stop the button and the flashing stops but
    when I start it again with the button the app stops by itself.
    xx=new Runnable()
    {
    @Override
    public void run()
    {
    synchronized (this)
    {
    while (true)
    {
    try {

    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    camera.setParameters(parameters);
    camera.stopPreview();

    c1.sleep(100);
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(parameters);
    camera.startPreview();
    //c1.interrupt();

    }
    catch (Exception e)
    {
    Thread.currentThread().interrupt();
    break; //this is a MUST

    }

    }
    }
    }
    //}

    };

    c1 =new Thread(xx);

    s1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (s1.isChecked())

    {
    c1.start();
    }
    else
    {
    c1.interrupt();

    }

    }

    });

  10. Hisham Allahem Reply

    Thank you very much for the video. Clear and easy to understand.

  11. Hi Prabeesh. Thank you for great tutorial . I have a question though. MyService class involves a callback ,LocationListener onLocationChanged, does the newly created thread handle the callback or main thread handles it ? Please let me khow

  12. Anton Horváth Reply

    YOUR TUTORIALS DOESNT MAKE ANY SENSE .. YOUR ENGLISH IS A CUP OF SHIT

  13. Amr Ashraf Reply

    why are you use syncronized function ? as it is in the same thread

  14. Gleide Gomes Reply

    Hi Prabeesh! I removed the code inside from run and why the service finish in aproximately 10 seconds alone? Thank you!

  15. deadpiece111 Reply

    i don't think that thread will stop coz it doesnot have intrrupt in it as for the service i know it will stop but Thread……nope…..

Write A Comment