Android scheduled files to server
Ok i know how to send a file to the server...What i am looking for is code which looks in a folder on the sd card and then sends all the text files in there and then deletes th开发者_如何学编程em..However it needs to keep looking in there until there is internet connection so that it completes the job, otherwise it keeps looking in there every 30 seconds..? hope i explained correctly
For me you should build a service which does those operations.
public class UploadService extends Service {
private WakeLock cpuLock;
private WifiLock wifiLock;
private UploadFilesTask task;
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
final PowerManager powerMgr = (PowerManager)getSystemService(Context.POWER_SERVICE);
cpuLock = powerMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_lock");
cpuLock.acquire();
final WifiManager wifiMgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, "my_lock");
wifiLock.acquire();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if ( intent.hasExtra("start") && taskIsNotRunning() ) {
task = new UploadFilesTask();
task.execute(null);
return;
}
}
private boolean taskIsNotRunning() {
return task == null || task.getStatus() != AsyncTask.Status.RUNNING || task.isCancelled();
}
@Override
public void onDestroy() {
super.onDestroy();
cpuLock.release();
wifiLock.release();
}
private class UploadFilesTask extends AsyncTask<Object, Object, Object> {
private String dirName;
public UploadFilesTask(String directoryName) {
dirName = directoryName;
}
@Override
protected Object doInBackground(Object... arg0) {
final File uploadDirectory = new File(Environment.getExternalStorageDirectory(), dirName);
final File filesToUpload[] = uploadDirectory.listFiles(new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt");
}
});
for ( File f : filesToUpload ) {
try {
// invoke your method for upload
f.delete();
} catch ( Exception e ) {};
}
return null;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
stopSelf();
}
}
}
Don't forget about putting service declaration into AndroidManifest.xml file.
You can launch the service with "boot complete" intent or directly from your activity.
However it needs to keep looking in there until there is internet connection so that it completes the job, otherwise it keeps looking in there every 30 seconds..? hope i explained correctly
Try to launch the service when wifi/3G internet connection appear. Your service can be triggered by this intent (You have to add intent-filter in AndroidManifest.xml file).
Let me put some advice about background operations:
add some rules to decrease the rate of service runs. In other case your battery will drain very quickly. Also consider AlarmManager.
check battery level before upload (BatteryManager).
check if you are in roaming mode and if user is fine with that (SystemSettings).
精彩评论