Android storage performance

I am actually developing an Android application with a data warehouse, and I continue like this:

Activity → Business Services → Repo (with Spring REST fw). Using this, I am forced to allow the activity to complete the storage job before closing it (thread processing, progress dialog ...).

Is this a bad coding method for using android service to store data?

At the same time, users can continue to move around and have the impression that they are working with a very flexible application. This is a good decision?

thank

+4
source share
1 answer

There is no need to keep your activities in the foreground, waiting for the completion of background logic.

, "" .

: .



class MyActivity extends Activity {

     void calledWhenActivityNeedsToBeClosed() {

          // start a thread to do background work
          new Thread() {
                public void run() {
                     perform long running logic here
                }
          }.start();

          // and clos the activity without waiting for the thread to complete
          this.finish();
     }
}

AsyncTask java.concurrent . .

. . .. .
? , () - ed, Android .
, , , . ?



, , , :

class MyActivity extends Activity {

     void calledWhenActivityNeedsToBeClosed() {

          // delegate long running work to service
          startService(this, new Intent(this, MyWorkerService.class));

          // and close the activity without waiting for the thread to complete
          this.finish();
     }

}


. Android , , , .


, , -, IntentService.


, , Android, . , , , , - , , , , :

static final int NOTIF_ID = 100;

// Create the FG service intent 
Intent intent = new Intent(getApplicationContext(), MyActivity.class); // set notification activity
showTaskIntent.setAction(Intent.ACTION_MAIN);
showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pIntent = PendingIntent.getActivity(
                getApplicationContext(),
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

Notification notif = new Notification.Builder(getApplicationContext())
                .setContentTitle(getString(R.string.app_name))
                .setContentText(contentText)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pIntent)
                .build();

startForeground(NOTIF_ID, notif);


+27

Source: https://habr.com/ru/post/1547180/


All Articles