我是初学者。我想要来自 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();
}
});