I had a similar problem to yours when i was a student. I largely mitigate against it with a few things. I found the smartest guy in the class and stuck with him, working with hard working people made me work harder - especially if they're relying on me to complete something.
When studying, I also just accepted the fact that i was going to waste a lot of time with my gadgets and made sure to allocate enough time overall to accommodate for it ( I know this is a luxury for some). As long as i had finished the task i set out to complete, i was happy - even if it took me double the amount of time it should have. I made it goal oriented - and not an exercise in itself
I went to the library or an empty computer lab to get my stuff done, I turned it into a schedule and went there even if i had nothing to do (Turns out, there's always something to do).
I had immense trouble focusing on lectures, i couldn't do much about it. So i became selective about the ones i attended but i always made sure to go to campus when the lecture was on and sit in the library, maybe reviewing the slides the professor put online for the week.
Essentially, you need to put yourself in situations where getting shit done is the least boring option. Unless you're in a completely wrong course you'll probably find some aspects of the work interesting in itself. There are only so many buzzfeed and reddit posts you can read a day..