在Android中使用Toast的上下文?

use*_*241 7 android android-context android-toast

我刚学会了我可以使用:

Toast.makeText(MainActivity.this, R.string.some_string,Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

要么,

Toast.makeText(getApplicationContext(), R.string.some_string,Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

在Android中显示Toast.

之前我认为上下文实际上是父窗口的一种句柄,应该显示它,但文档不清楚这一点.

然后我遇到了这张桌子: 在此输入图像描述

它似乎也没有提到准确用于Toast的上下文?

编辑:

像Toast这样的子窗口的上下文是"父窗口的句柄"吗?或者它实际上允许Toast.makeText访问资源或什么?

如果上下文无关紧要,为什么要使用呢?

Ale*_*der 0

您只能从 UI(主线程)上下文中显示 Toast。如果您想从服务中显示此内容(但这与 Google 指南相矛盾),您可以这样做:在服务中的当前活动中显示 toast

  • 谷歌使用的应用程序上下文:http://developer.android.com/guide/topics/ui/notifiers/toasts.html (2认同)