0

I have a problem, i need download an different image every day, and I've used this code but this work one day and other day dont work... i dont know, help me please, what is the problem.

public class DownloadImageToSdcard {

private String URL_PHOTO = "/{mysite}.com/myimage/";
@SuppressLint("SdCardPath")
private String DIR_FOLDER = "/sdcard/myphotos/";

Calendar calendar = Calendar.getInstance();

public void MakeFolderToPhoto(String NAME_PHOTO) throws FileNotFoundException {

    File myDir = new File(DIR_FOLDER);

    if(!myDir.exists())
    {
         myDir.mkdirs();
    }

    try{
        URL url = new URL(URL_PHOTO+NAME_PHOTO);

        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = NAME_PHOTO;
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 

             /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();
            InputStream inputStream = null;
           HttpURLConnection httpConn = (HttpURLConnection)ucon;
          httpConn.setRequestMethod("GET");
          httpConn.connect();

          if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
           inputStream = httpConn.getInputStream();
          }

            FileOutputStream fos = new FileOutputStream(file);
            int size = 1024*1024;
            byte[] buf = new byte[size];
            int byteRead;
            while (((byteRead = inputStream.read(buf)) != -1)) {
                fos.write(buf, 0, byteRead);
            }

            fos.close();

    }catch(IOException io)
    {
    }
    catch(Exception e)
    {   
        e.printStackTrace();
    }
}

}

and i call this function in my MyWallService.java of WallpaperService, i want call to download every day 12:10am and save this in Sdcard

void UpdateWall(int hour, int minut) {

        if(hour == 0)
        {
            if(minut == 10)
            {
                        DownloadImage.MakeFolderToPhoto(NAME_IMAGE_PREF+Integer.toString(calendar.get(Calendar.DAY_OF_MONTH))+".jpg");
                    }
             }
 }
EliasM
  • 737
  • 1
  • 6
  • 14

2 Answers2

0

You should use AlarmManager: http://developer.android.com/training/scheduling/alarms.html.

Volodymyr
  • 1,037
  • 1
  • 11
  • 27
0

You will need to set an Alarm using AlarmReceiver here is some example code

There is a couple of other things you will need to do though

  • You will also need a boot_completed receiver to set the alarm after a phone reboot.
  • You will likely need to save the last download time in a preference so that you can accurately determine success.
public class DownloadAlarmReceiver extends BroadcastReceiver {
  private static final int INTERVAL_ALARM = 24*60*60*1000; // 1hr 
  @Override
  public void onReceive(Context context, Intent intent) {
      Intent i = new Intent(context, WallpaperService.class);
      i.setAction(Globals.INTENT_DOWNLOAD_IMAGE);
      context.startService(i);    
  }
    public static void setAlarm(Context act) {
        Intent intent = new Intent(act, DownloadAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(act, Globals.ALARM_REQUEST_CODE, intent, 0);

        AlarmManager alarmManager = (AlarmManager) act.getSystemService(Activity.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+INTERVAL_ALARM, INTERVAL_ALARM, pendingIntent);
    }

    public static void cancelAlarm(Context act) {
        Intent intent = new Intent(act, DownloadAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(act, Globals.ALARM_REQUEST_CODE, intent, 0);

        AlarmManager alarmManager = (AlarmManager) act.getSystemService(Activity.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }
}

add this in the manifest to enable it:

<receiver android:name="com.ex.DownloadAlarmReceiver" />

for boot completed:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
1baga
  • 340
  • 4
  • 16
siliconeagle
  • 7,379
  • 3
  • 29
  • 41