0

I have an Activity, the user can start an background network task. The activity needs communicate with the task (both directions).

If the activity is killed and the task needs user input, the task should throw an notification for relaunch activity.

So. What item should be "the task? A service? Intent service?

amchacon
  • 1,891
  • 1
  • 18
  • 29
  • Depends on how often or long the "task" will be running, but you should look at https://developer.android.com/reference/android/os/AsyncTask.html – Tyler Sebastian Sep 19 '16 at 16:51

2 Answers2

2

The task can be a Broadcast Receiver to relaunch the activity. Check this:

Android - launch app from broadcast receiver

BroadCast Receiver Documentation

Community
  • 1
  • 1
A.J
  • 726
  • 1
  • 7
  • 19
1

The background task could be a Service, IntentService, or an AsyncTask. The problem is receiving user input, which must be done in an activity. So you must tell the activity to restart itself from the background thread. This could be done by a broadcast receiver or a handler (with weak reference).

I will use a service, activity and handler like below:

public class ActivityMain extends Activity 
{
    public static MyService myService;    
    private MyHandler myHandler = new MyHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startService(MyService.class, myConnection, null); 
    }

    private static class MyHandler extends Handler {
        private final WeakReference<ActivityMain> activityMain;

        public MyHandler(ActivityMain activity) {
            activityMain = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MyService.NEED_USER_INPUT:
                    final Intent i = new Intent(activityMain, ActivityMain.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(i);                   
                    break;
            }
        }
    } 

    private final ServiceConnection myConnection = new ServiceConnection()
    {
        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1)
        {
            myService = ((MyService.UsbBinder) arg1).getService();
            myService.setHandler(myHandler);
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0)
        {
            myService = null;
        }
    };

    private void startService(Class<?> service, ServiceConnection serviceConnection, Bundle extras)
    {
        if(MyService.SERVICE_CONNECTED == false)
        {
            Intent startService = new Intent(this, service);
            if(extras != null && !extras.isEmpty())
            {
                Set<String> keys = extras.keySet();
                for(String key: keys)
                {
                    String extra = extras.getString(key);
                    startService.putExtra(key, extra);
                }
            }
            startService(startService);
        }
        Intent bindingIntent = new Intent(this, service);
        bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
}

and the service:

 public class MyService extends Service
    {
        public static final int NEED_USER_INPUT = 0;
        public static boolean SERVICE_CONNECTED = false;
        private IBinder binder = new UsbBinder();
        private Context context;
        private Handler myHandler;

        @Override
        public void onCreate()
        {
            this.context = this;
        }

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

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

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

        public void setHandler(Handler myHandler)
        {
            this.myHandler = myHandler;
        }

        public class UsbBinder extends Binder
        {
            public MyService getService()
            {
                return MyService.this;
            }
        }

        private needUserInput() //call when you need to restart activity
        {
            if(myHandler != null)
                myHandler.obtainMessage(NEED_USER_INPUT).sendToTarget();

        }
    }
ahmad zen
  • 26
  • 1
  • 3