19
AlgoRythm
36d

Just did my first JobIntentService on Android. Hoo, boy.

The problem: I need to send a network request.

The issue: Android.

Of course, you can't do network on the main thread. That's silly in any application. Android really does try to punish you, though. The Android lifecycle can really fuck you over here. Imagine a long-running network operation, like 15 seconds. Plenty of time for the user to do something silly, like rotate the screen.

If you opened up a good old new Thread from Java, you'd get a crash because of a screen rotation. Same thing with Android's AsyncTask, which is the top answer on StackOverflow. AsyncTask is made for things that will take no longer than a few seconds (less than 5!). Network, especially cell network, can take longer.

So the solution? Create a JobIntentService class. It's a service, it will run in the background. You need to register it in your Android Manifest and ask for a new permission (wake lock). You need to implement another class for the receiver, and then you need to go to your activity and implement the receiver interface you just wrote.

Just. For. A. Network. Request!

And as far as I'm aware, this isn't even that bad considering the rest of Android's bullshit.

What a headache!

Comments
  • 7
    Android development is just so fucking obnoxiously complex man
  • 0
    My preference is just to rely on schedulers and issue async work as needed. Thread is definitely an optimization nowadays.

    Or I guess volley works too.

    https://developer.android.com/train...

    https://developer.android.com/refer...
  • 2
    I would say Android was specifically designed to make big thinks work, but doesn't care about small ones.
    The separation in e.g. Activities for Foreground, Services for Background are useful for larger projects, as they enforce this separation by Design. For simple things, this overly complicates everything, when not even necessary. On the other hand "going bigger" is easier then.
  • 3
    I know your pain.

    Fuck Android's life cycle!

    Xamarin Forms makes Android apps run in a single Activity and sets it to keep alive on rotation just to avoid most of the bullshit.
  • 2
    @SortOfTested is there anything you and @root don’t know?

    PS fuck android.
  • 1
    Why does Google have to do every android api in the most complex way
  • 1
    I feel your pain. I have realised that if an app requires a lot of cloud data handling and making internet requests, then having pwa or other non native option is the best. Native framework has yet to figure out better ways for dealing with the internet.

    Although if you want to make a data requests that could take upto 15 seconds, then foreground service with a notification might be a the most valid solution. Other options in no particular order would be job schedular/work manager , retrofit+okhttp / volley / other network handling library

    Also, asynctask was/is shit and now deprecated. And dealing with threads also don't give much better results. So try to stay away from them
  • 0
    @StopWastingTime AFAIK a lot of the new APIs (including JobIntent, from reading my error stacks!) Use AsyncTask under the hood.
Add Comment