There is no need to keep your activities in the foreground, waiting for the completion of background logic.
, "" .
: .
class MyActivity extends Activity {
void calledWhenActivityNeedsToBeClosed() {
new Thread() {
public void run() {
perform long running logic here
}
}.start();
this.finish();
}
}
AsyncTask java.concurrent . .
. . .. .
? , () - ed, Android .
, , ,
. ?
, , , :
class MyActivity extends Activity {
void calledWhenActivityNeedsToBeClosed() {
startService(this, new Intent(this, MyWorkerService.class));
this.finish();
}
}
. Android , , , .
, , -,
IntentService.
, , Android,
. , , , , - , , , , :
static final int NOTIF_ID = 100;
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
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);