0

I study services on Android and in the example I came across a code in which after a while a message comes from the service. But I can’t understand why I get the dialog "MyActivity has stopped" (link to the image below) instead of a notification. I found the reason and this is the commented out line:

//Toast.makeText(this, "onCreate: Service work!", Toast.LENGTH_SHORT).show();

My code:

public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        //Toast.makeText(this, "onCreate: Service work!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            TimeUnit.SECONDS.sleep(3);
            Toast.makeText(MyService.this, "onStartCommand: Ding!", Toast.LENGTH_SHORT).show();
        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }
}

I started my servece from:

startService(new Intent(this,MyService.class));

In my AndroidManifest service registred as:

<service android:name=".MyService" android:process=":com.myService"/>

Question:

Please explain why I get this message when I use Toast in onCreate().

https://i.stack.imgur.com/VlHQ9.png

Dmitry
  • 11
  • 1
  • 1
    Please check LogCat in Android Studio and post the error here. – Zohaib Amir Aug 04 '19 at 15:36
  • Check this https://stackoverflow.com/a/5420929/9263083 – sanoJ Aug 04 '19 at 15:36
  • @sanoJ, you propose creating a Toast in a different thread, but this also calls "my" message `@Override public void onCreate() { super.onCreate(); new Thread(new Runnable(){ @Override public void run() { Toast.makeText(MyService.this, "onCreate: Service work!", Toast.LENGTH_SHORT).show(); } }).start(); }` – Dmitry Aug 04 '19 at 15:43
  • @Zohaib Amir, when the service starts and when I receive the message - the log is empty. Maybe LogCat needs to be configured somehow, but messages only go when building the application and that’s it – Dmitry Aug 04 '19 at 15:47

1 Answers1

1

Your service might be in a different thread. Use handler to show Toast:

Handler handler = new Handler();
Runnable toastRunnable = new Runnable(){ 
@Override public void run() { 
Toast.makeText(MyService.this, "onCreate: Service work!", Toast.LENGTH_SHORT).show(); 
    } 
};

handler.post(toastRunnable);
Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
  • very strange, I see the message `onCreate:..` after the message `onStartCommands :...`. But I could always receive `Toast` messages, why now I need a thread? – Dmitry Aug 04 '19 at 16:30
  • 1
    Toast messages are meant to be run only on UI Thread or Main Thread... Services are also run on UI Thread by default. But, when you are creating the service (startService), you might have done that on some other thread and trying to show the toast which makes it run on some other thread than the UI thread that makes the app to crash. – Farhan Ibn Wahid Aug 04 '19 at 16:38
  • 1
    The handler just makes the toast to run on UI Thread, that's the reason you now need the handler to show the toast. In fact, it's what the handler does! – Farhan Ibn Wahid Aug 04 '19 at 16:39
  • oh thanks this answers my question completely thanks – Dmitry Aug 04 '19 at 16:46