0

I would like to call a non-static method in the for-loop inside the onHandleWork. How can I achieve that?

@Override
protected void onHandleWork(@NonNull Intent intent) {
    Log.d(TAG, "onHandleWork");

    String input = intent.getStringExtra("inputExtra");


    for (int i = 0; i < 10; i++) {
        Log.d(TAG, input + " - " + i);

        new MainActivity.method();

        if (isStopped()) return;

        SystemClock.sleep(1000);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tester2389
  • 149
  • 1
  • 12
  • What does this method do? Can you move it out of your Activity? If it's intrinsically tied to your activity in some way then you'll need to launch it and conduct your work there. – PPartisan Nov 24 '19 at 12:07
  • It interacts with the UI, so it's not possible to get it out of there – Tester2389 Nov 24 '19 at 12:09
  • Ok - in that case then I'd launch your `Activity` and conduct any work in there – PPartisan Nov 24 '19 at 12:12
  • Or would you recommend me to use a ForegroundService in case you can call activity methods there? – Tester2389 Nov 24 '19 at 12:13
  • You can't. My advice would be that if you can separate this logic from your Activity, then do that and conduct it in your service as it's preferable. If, however, that isn't possible (i.e., the logic you want to run requires user input/a UI), then you have no choice but to conduct it with the Activity. – PPartisan Nov 24 '19 at 12:15
  • And what would you say about using an Handler or is there no way to effectively keep it alive in the background while a "heavy" external work is running in the meantime? – Tester2389 Nov 24 '19 at 12:19
  • It would help if you posted more of your code. It's difficult to make anything more than a general recommendation based on this information alone. – PPartisan Nov 24 '19 at 12:21
  • Well, I now checked the task again and it would be sufficent to only show a Toast message and send a Notification within the JobIntentService. Would those two things be possible with the JobIntentService? – Tester2389 Nov 24 '19 at 12:36
  • Yes - you can do both of those things directly from a `Service`, without an `Activity` – PPartisan Nov 24 '19 at 12:43
  • I just tried it and it worked, however I see that the JobIntentService sometimes stops working in the background while I am doing "heavy" work like installing big 100MB apps. Should I just call onHandleWork again within onDestroy or onStopWorking in those scenarios? – Tester2389 Nov 24 '19 at 13:15
  • According to [this answer](https://stackoverflow.com/a/51994252/1219389), your `JobIntentService` should run for ten minutes. You can consider using something other than a `JobIntentService` for very long downloads, maybe `DownloadManager`. – PPartisan Nov 24 '19 at 13:21
  • Or a `ForegroundService`** – PPartisan Nov 24 '19 at 13:26

1 Answers1

0

You should broadcast a message that the activity is listening instead.

To achieve that, use a broadcastreceiver on the activity and send It from the job service with context.sendBroadcast(intent)

You can make your activity listening by declaring a broadcastreceiver programatically or in the AndroidManifest.xml file. Note that If you declare it programatically, the activity Will only receive the broadcast If It is opened. If you use the manifest approach, when you activity is not opened and you send a broadcast, the sustém os going to start your activity

Receiving Broadcasts: https://developer.android.com/guide/components/broadcasts.html#manifest-declared-receivers

Sending Broadcasts: https://developer.android.com/guide/components/broadcasts.html#sending-broadcasts

Docs: https://developer.android.com/reference/android/content/BroadcastReceiver

Rander Gabriel
  • 637
  • 4
  • 14
  • I generally could use a receiver for my task, however is there a way to register the BroadCast in the activity and keep it alive while the app is in background while some external work is being done? I namely would like to get a callback after this special task which might be heavy work on some devices. – Tester2389 Nov 24 '19 at 12:12
  • If you register your receiver inside your AndroidManifest file It Will start your activity If It is not running – Rander Gabriel Nov 24 '19 at 12:16