2

I have a MyServiceClass defined as follows:

public class MyService extends Service {
    public MyService() {
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}

If I call startService(new Intent(getBaseContext(), MyService.class)); from an activity class in the same package/app/APK, then I can see the Toast message.

But if I put this class in an application with no activity whatsoever (that is, service-only application) by simply tying it to boot receiver:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, com.example.tutorialspoint7.noactivity.MyService.class));
    }
}

Then, when the service starts, I no longer see the message.

I can restart the service on demand via Package Browser:

enter image description here

I understand that if there is no activity to provide a UI, then those messages don't really have where to be displayed. My questions, though, are:

  1. Is there a default place where I can find these messages? (e.g. log file, buffer, LogCat, etc.)
  2. Can I redirect these messages to the home screen current screen?
  3. Why isn't the Android Studio framework display a warning when it sees/builds an APK that contains Toast.makeText().show() messages that have nowhere to be displayed?
Jay Souper
  • 2,576
  • 2
  • 14
  • 17
  • http://stackoverflow.com/questions/3134683/android-toast-in-a-thread – Kuffs Mar 20 '17 at 11:02
  • Just to answer quickly to your last question: Toast.makeText() takes as a parameter the Activity on which the toast will be shown. What are you giving as an activity? If you give `null` well, the framework does tell you that something is not gonna work. If you are giving a real activity it has nothing to say, because it thinks that that activity will be alive at runtime (no reason for it to think otherwise). – Simone Chelo Mar 20 '17 at 11:04
  • 3
    @SimoneChelo, you are wrong. `Toast.makeText()` does not take Activity as a parameter. It does take a `Context`. – Vladyslav Matviienko Mar 20 '17 at 11:08
  • you are right, my bad. Then yes, I share OP's doubts. – Simone Chelo Mar 20 '17 at 11:12
  • Normally an application without an `Activity` is in the "stopped state" and its `BroadcastReceiver`s will not be triggered while the application is in this state. The application needs to be started (at least once) manually by the user. You may not be seeing the `Toast` because the `Service` may not actually be running. You should add debug logging and check your logcat to see what is really happening. – David Wasser Mar 21 '17 at 14:32

2 Answers2

3

The Toast messages are not related to your activity, but it a service on the Android UI which can be accessed by any application/activtiy. A simple glance at the source code would tell you that. So if you pass the application context by getApplicationContext(), it will display from an activity-less application too.

FYI: The toast is not bound to the UI of your activity. If you display a toast from your activity and then minimize it(press home), the toast remains on the home screen.

No, you cannot see toast messages that didnt get displayed because they were not enqued in the service itself.

Regarding android studio warning, I'm not sure why it doesn't report it, you could raise an issue regarding the same. But I had read that android developers suggest using the application context in all instances, even when an activity context is available. Sorry i cannot find the source from where I read this.

Harsh Ganatra
  • 411
  • 3
  • 8
0

Use getApplicationContext() to access an activity-free context

Mehmet K
  • 2,805
  • 1
  • 23
  • 35
  • 2
    Service is a `context` as well so what would it change? – Than Mar 20 '17 at 11:18
  • Aggree with @Than. Service has it's own Context, which has to work for toasts as well – Vladyslav Matviienko Mar 20 '17 at 11:20
  • @JaySouper technically you shouldn't be asking more than one question in one Question https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts – Mehmet K Mar 20 '17 at 11:29
  • No your last 3 points are clearly 3 different questions. Their answers are not mutual or dependent. They each can be an individual question on this site, – Mehmet K Mar 20 '17 at 11:37