【问题标题】:Toast from JobIntentService来自 JobIntentService 的吐司
【发布时间】:2019-11-21 23:24:48
【问题描述】:

我是初学者。 我想要来自 jobintentservice (onHandleWork) 的 Toast,但我的应用程序崩溃了。 logcat 错误是:“不能在没有调用 looper.prepare() 的线程上烤面包” 我想在 jobintentservice 中与处理程序一起学习 请帮帮我。

public class NotificationService extends JobIntentService {
    public static final String TAG = NotificationService.class.getSimpleName();
    Handler handler;

    public static void enqueuWork(Context context,Intent intent){
        enqueueWork(context,NotificationService.class,20,intent);
    }
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Start background Service", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        handler = new Handler();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        handler.post(new Runnable() {
          @Override
          public void run()
              Toast.makeText(NotificationService.this, "onhandlework", Toast.LENGTH_SHORT).show();

          }
      });

【问题讨论】:

    标签: android-handler android-toast android-looper jobintentservice


    【解决方案1】:

    你现在已经猜到了,但对于未来的读者来说,错误“不能在没有调用 looper.prepare() 的线程上烤”只是意味着你必须从 ui 线程调用相关的 ui。

    activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        showToast(activity);
    }});
    

    您可以找到类似的问题herehere

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 1970-01-01
      • 2011-08-02
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      相关资源
      最近更新 更多