Android create handler inside thread into Service

user4001049

i'm writing simple android service and i want to use such as Toast or Notification but i get this error:

 FATAL EXCEPTION: Thread-17116
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

i can not use runOnUiThread . my service does not know that. for example i try to use that with : this, getBaseContect(), getApplication, mContext for .runOnUiThread(new Runnable() {}

i get problem and i can not resolve problem.

this is my code:

public class TsmsService extends Service {

    private Timer smsThread;
    private DatabaseHandler db;
    private SQLiteDatabase dbHelper;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        smsThread = new Timer();
        GetSMSThread getSMSThread = new GetSMSThread(getBaseContext());
        smsThread.scheduleAtFixedRate(getSMSThread, 0, 1000); //(timertask,delay,period)
        return super.onStartCommand(intent, flags, startId);
    }

    public class GetSMSThread extends TimerTask {
        private Context mContext;

        public GetSMSThread(Context context) {
            mContext = context;
        }

        @Override
        public void run() {

            this.runOnUiThread(new  Runnable() {
                public  void  run() {
                    Toast.makeText(getApplication() , "Service is Running ... " , Toast.LENGTH_SHORT).show();
                }
            });

        }
    }
}
eshayne

Try creating a Handler in onStartCommand (so, from the UI thread). Then use that Handler to trigger the Toast. For example:

private Handler mToastHandler = null;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    mToastHandler = new Handler();
    ...
}

...

    // from inside your child thread
    mToastHandler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(...);
        }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't create handler inside thread - Android

From Dev

Can't create handler inside thread that has not called Looper.prepare() android service

From Dev

Android Background Service Error : Can't create handler inside thread that has not called

From Dev

Android service with handler and thread

From Dev

Android thread error - Can't create handler inside thread that has not called Looper.prepare()

From Dev

Android - Can't create handler inside thread that has not called Looper.prepare()

From Dev

android Can't create handler inside thread that has not called Looper.prepare

From Dev

Android: Can't create handler inside thread that has not called Looper.prepare() Running new threads

From Dev

Android client: Can't create handler inside thread that has not called Looper.prepare()

From Dev

Android : Can't create handler inside thread that has not called Looper.prepare()

From Dev

Android nullpointer exception and Can't create handler inside thread that has not called Looper.prepare()

From Dev

Android AsyncTask Error [Can't create handler inside thread that has not called Looper.prepare()]

From Dev

Thread: Can't create handler inside thread that has not called

From Dev

Error inserting data in Mysql fom android:Android Can't create handler inside thread that has not called Looper.prepare() in AsyncTask

From Dev

thread doesn't run inside android service

From Dev

Android App Crashes when no internet connection due to Can't create handler inside thread that has not called Looper.prepare()

From Dev

Android App Crashes when no internet connection due to Can't create handler inside thread that has not called Looper.prepare()

From Dev

Android pausing Service, Thread, Asynctask? Using a handler with postdelayed for this?

From Dev

Handler and Thread inside Service keeps running, while the Service seems to have stopped

From Dev

Create a Handler inside doInBackground

From Dev

Will handler.post(new Runnable()); create new Thread in Android?

From Dev

Android: Why can't I create a handler in new thread

From Dev

Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread

From Dev

Where to create own thread, Inside service class or Inside the activity form where we give call to the service

From Dev

Handler postDelayed in Android Service

From Dev

handler or alarm in android service?

From Dev

Create signal handler for a single thread

From Java

Can't create handler inside thread that has not called Looper.prepare()

From Dev

Yet another "Can't create handler inside thread that has not called Looper.prepare()" topic

Related Related

  1. 1

    Can't create handler inside thread - Android

  2. 2

    Can't create handler inside thread that has not called Looper.prepare() android service

  3. 3

    Android Background Service Error : Can't create handler inside thread that has not called

  4. 4

    Android service with handler and thread

  5. 5

    Android thread error - Can't create handler inside thread that has not called Looper.prepare()

  6. 6

    Android - Can't create handler inside thread that has not called Looper.prepare()

  7. 7

    android Can't create handler inside thread that has not called Looper.prepare

  8. 8

    Android: Can't create handler inside thread that has not called Looper.prepare() Running new threads

  9. 9

    Android client: Can't create handler inside thread that has not called Looper.prepare()

  10. 10

    Android : Can't create handler inside thread that has not called Looper.prepare()

  11. 11

    Android nullpointer exception and Can't create handler inside thread that has not called Looper.prepare()

  12. 12

    Android AsyncTask Error [Can't create handler inside thread that has not called Looper.prepare()]

  13. 13

    Thread: Can't create handler inside thread that has not called

  14. 14

    Error inserting data in Mysql fom android:Android Can't create handler inside thread that has not called Looper.prepare() in AsyncTask

  15. 15

    thread doesn't run inside android service

  16. 16

    Android App Crashes when no internet connection due to Can't create handler inside thread that has not called Looper.prepare()

  17. 17

    Android App Crashes when no internet connection due to Can't create handler inside thread that has not called Looper.prepare()

  18. 18

    Android pausing Service, Thread, Asynctask? Using a handler with postdelayed for this?

  19. 19

    Handler and Thread inside Service keeps running, while the Service seems to have stopped

  20. 20

    Create a Handler inside doInBackground

  21. 21

    Will handler.post(new Runnable()); create new Thread in Android?

  22. 22

    Android: Why can't I create a handler in new thread

  23. 23

    Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread

  24. 24

    Where to create own thread, Inside service class or Inside the activity form where we give call to the service

  25. 25

    Handler postDelayed in Android Service

  26. 26

    handler or alarm in android service?

  27. 27

    Create signal handler for a single thread

  28. 28

    Can't create handler inside thread that has not called Looper.prepare()

  29. 29

    Yet another "Can't create handler inside thread that has not called Looper.prepare()" topic

HotTag

Archive