Control of Activities

2

Hello, I have the following problem. In a Activity I have a button that executes a AsyncTask . The problem is that if the app is completely closed, the operation is not executed.

Is it possible to call an Asynchronous method when applications are closed? I was testing with the onDestroy method in my Activity in this way:

@Override
protected void onDestroy() {
    Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
    super.onDestroy();
}

But he does not show me the Toast message when he closes it. Am I implemented poorly? Thanks in advance.

    
asked by Megaherx 01.12.2017 в 16:05
source

2 answers

5

In your case it seems that you need to use Service , since the AsyncTask is destroyed when your application is closed if you use the context of the activity, you can do it in the following way:

Create your service class:

public class MyService extends Service {
    private final LocalBinder mBinder = new LocalBinder();
    protected Handler handler;
    protected Toast mToast;

    public class LocalBinder extends Binder {
        public SendDataService getService() {
            return SendDataService .this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() { 

                   //realiza tu tarea asíncrona aquí
            }
        });
        return android.app.Service.START_STICKY;
    }

}

Declare it inside the AndroidManifest.xml

<service
    android:name="MyService"
    android:label="@string/service_name"
    >
</service>

Then you start it:

Intent i= new Intent(context, MyService.class);
context.startService(i);

For more information I leave this link:

link

    
answered by 01.12.2017 / 16:21
source
1
  

It is possible to call an Asynchronous method when they are closed   Applications? I was testing with the onDestroy method in my Activity   in this way:

@Override
protected void onDestroy() {
    Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
    super.onDestroy();
}
  

But he does not show me the Toast message when he closes it. I am   implemented poorly? Thanks in advance.

The messages Toast should be displayed on a context, in case your code is about Activity ( this ), if the application is closed, the Toast will not have a base on where to show, for example an Activity.

If you want to execute a method after closing an application as an option you have the one of create a service , which can continue to close the application, in this case defining Service.START_STICKY

   public int onStartCommand(Intent intent, int flags, int startId) {
        ...
        return Service.START_STICKY;
    }

In this question you will find an example of a service that is still running, even if the application is closed:

services on android

On the site you can currently find a lot of information about services on Android:

link

    
answered by 01.12.2017 в 17:12