【发布时间】: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