0

I am uploading a huge video file by android service. But when internet fail during the uploading time then uploading failed. But want to upload restart if internet comes if the service still running. How I can achieve this things. Please help me out. Here is the service code.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        currentId=startId;
        receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
        allpath=intent.getExtras().getStringArray("stringarray");
         selectedfilename=intent.getExtras().getStringArray("selectedfilename");


        boolean isinternet=ImageUtil.isInternetOn(getApplicationContext());
        if (isinternet) {

            if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
                new UploadingTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            } else {
                new UploadingTask().execute();
            }
        }


        return Service.START_REDELIVER_INTENT;
    }
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • If internet failed then `AsyncTask` throw some error catch that error and start uploading video again. and after uploaded successfully you can delete it or store some flag in `DB` for just notifying this Video already uploaded in server. – M D Apr 08 '15 at 07:49
  • yes it throw error but how i can restart again uploading in service class can you give trick of code it will help me – Sunil Kumar Apr 08 '15 at 07:51
  • I do not have a code.. and never restart service again. user `TimerTask` that will call if your uploading failed. – M D Apr 08 '15 at 08:47

1 Answers1

0

You can register for CONNECTIVITY_ACTION to receive broadcast receiver. ConnectivityManager is the class you should be referring. You can start your download and if it fails you can set a flag. On receiving the CONNECTIVITY_ACTION broadcast you can check the flag and start your service again based on your logic. This answer might be helpful in implementing. Hope this helps.

Community
  • 1
  • 1
Saurav
  • 560
  • 4
  • 17
  • thanks but in start service how i can send the intent data to service. intent data only available to activity calss – Sunil Kumar Apr 08 '15 at 08:09
  • You can register receiver via manifest of your XML. In your service when you start downloading you can detect if the download failed or not. On failing you can set a flag, then on the receiver of the same broadcast(CONNECTIVITY_ACTION) you can restart your service. You don't need activity to achieve this. – Saurav Apr 08 '15 at 11:47