0

I want to upload images (using asyncTask),but if my internet is down,i want to send a broadcast intent and upload images automatically when internet is connected next time. I have successfully uploaded images when internet is active, I saw many examples but none of them helped me.

i have a class UploadImages which extends AsyncTask

I made broadcast receiver class like this

public class MyBroadcastReceiver extends BroadcastReceiver {    
  ConnectivityManager connec;

  @Override
  public void onReceive(Context context, Intent intent) {
      connec = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
      if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED))
      {
            Intent intent1 = new Intent(context, UploadIntentService.class);
            intent1.putExtra("", "index.html");
            context.startService(intent);
    }
  } 
}

Here is my UploadIntentService class

public UploadIntentService() {
    super("uploadIntentService");   
}

@Override
protected void onHandleIntent(Intent intent) {
    new UploadImageToBucket().execute("");
    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("com.alg.imgSave.MyBroadcastReceiver");
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("RESPONSE_STRING", "1");
    broadcastIntent.putExtra("RESPONSE_MESSAGE", "Uploaded");
    sendBroadcast(broadcastIntent);
}

But this is not working

Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
  • I think you shouldn't send an intent. You need to make broadcast reciever that handle messge from `ConnectivityManager`. – Wishmaster Apr 09 '15 at 11:44

1 Answers1

0

Please reference:

Broadcast receiver for checking internet connection in android app

The idea here would be that if your internet goes down, send the broadcast intent which this receiver is registered for, and once your internet connection changes this receiver will be launched and then you can make your request to upload the image.

Community
  • 1
  • 1
kandroidj
  • 13,784
  • 5
  • 64
  • 76