4

I develop an application that has background tasks on C++. The tasks are working during 1 minute. The tasks are invoked every 5 minutes by IntentServiсe. If application was unloaded, my IntentService load shared library and called native tasks successfully.

But when application is running and tasks were invoked, and after that I stopped application, tasks were stopped too.

So I need the way to hold native tasks after application was stopped.

Please, help me, if You have any ideas.

vgonisanz
  • 11,831
  • 13
  • 78
  • 130

1 Answers1

3

I am not sure,

But I think the service option that you have chosen is not the best option (Intent service), which is oriented to short tasks and besides if I am wrong the service stops when the app is destroyed.

see:

Service vs IntentService

https://developer.android.com/guide/components/services.html

In your case, you need a service could be bindable using IPC (Inter Process Communication). or RPC calls.

In my opinion, you need a basic service:

public class BasicService extends Service {
    public BasicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My service is created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        // For time consuming an long tasks you can launch a new thread here...
        Toast.makeText(this, " Starting service", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    }
}

One last thing, you will need to stop manually using, because you want not to make stopSelf() function.

 stopService(new Intent(this, BasicService.class));

Finally if you want to start a service:

startService(new Intent(this, BasicService.class));

Hope this helps.

Cheers.

Unai.

Community
  • 1
  • 1
uelordi
  • 2,189
  • 3
  • 21
  • 36
  • If I start a simple loop in IntentService, it did not stop if I stopped application. So I think, that my problem is in unloading native library when application stopped. But I don't know, how fix it. – user7378684 Jan 05 '17 at 13:46
  • without code I cannot see your problem. Could you post and organize your code in your question, please? – uelordi Jan 05 '17 at 14:04