Android 在 IntentService 中创建的 Toast 永远不会消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3296639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:33:30  来源:igfitidea点击:

Toast created in an IntentService never goes away

android

提问by jax

I have an IntentService that downloads some files. The problem is that I create a Toast inside the IntentService like this

我有一个 IntentService 可以下载一些文件。问题是我像这样在 IntentService 中创建了一个 Toast

Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT).show();

The Toast will never disappear event if I exit the app. The only way to destroy it is to kill the process.

如果我退出应用程序,Toa​​st 永远不会消失事件。破坏它的唯一方法是终止进程。

What am I doing wrong?

我究竟做错了什么?

采纳答案by Rich Schuler

You shouldn't create Toasts from a Service. You should use a Notificationinstead.

你不应该Toast从 a创建s Service。你应该使用 aNotification代替。

回答by rony l

The problem is that IntentServiceis not running on the main application thread. you need to obtain a Handlerfor the main thread (in onCreate()) and post the Toastto it as a Runnable.

问题是IntentService没有在主应用程序线程上运行。您需要Handler为主线程(in onCreate())获取 a并将Toast其作为Runnable.

the following code should do the trick:

以下代码应该可以解决问题:

@Override
public void onCreate() {
    super.onCreate();
    mHandler = new Handler();
}

@Override
protected void onHandleIntent(Intent intent) {
    mHandler.post(new Runnable() {            
        @Override
        public void run() {
            Toast.makeText(MyIntentService.this, "Hello Toast!", Toast.LENGTH_LONG).show();                
        }
    });
}

回答by Ton

This works for me:

这对我有用:

public void ShowToastInIntentService(final String sText) {
    final Context MyContext = this;

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Toast toast1 = Toast.makeText(MyContext, sText, Toast.LENGTH_LONG);
            toast1.show();
        }
    });
};

回答by bluesway

IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.

IntentService 将创建一个线程来处理新的意图,并在任务完成后立即终止它。因此,Toast 将不受死线程的控制。

You should see some exceptions in the console when the toast showing on the screen.

当 Toast 显示在屏幕上时,您应该会在控制台中看到一些异常。

回答by Bugs Happen

For people developing in Xamarin studio, this is how its done there:

对于在 Xamarin 工作室开发的人来说,这是在那里完成的:

Handler handler = new Handler ();
handler.Post (() => {
    Toast.MakeText (_Context, "Your text here.", ToastLength.Short).Show ();
});

回答by Daniel De León

To show a toast when the user is in one of the application activity.

当用户处于应用程序活动之一时显示祝酒词。

Just need a reference of the current activity, and call it with this sample code:

只需要当前活动的引用,并使用以下示例代码调用它:

public void showToast(final String msg) {
    final Activity a = currentActivity;
    if (a != null ) {
        a.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(a, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

There is a lot of options to get the current activity, check this question: How to get current foreground activity context in android?

有很多选项可以获取当前活动,请检查这个问题: 如何在 android 中获取当前的前台活动上下文?

But I use this approach:

但我使用这种方法:

The application must have:

该应用程序必须具有:

private Activity currentActivity = null;

public Activity getCurrentActivity() {
    return currentActivity;
}

public void setCurrentActivity(Activity mCurrentActivity) {
    this.currentActivity = mCurrentActivity;
}

Each activity must have:

每项活动必须具有:

@Override
protected void onResume() {
    super.onResume();
    ((MyApplication) getApplication()).setCurrentActivity(this);

}
@Override
protected void onPause() {
    super.onPause();
    ((MyApplication) getApplication()).setCurrentActivity(null);
}