Skip to content

Instantly share code, notes, and snippets.

@jcarlos7121
Created November 24, 2012 08:05
Show Gist options
  • Select an option

  • Save jcarlos7121/4138840 to your computer and use it in GitHub Desktop.

Select an option

Save jcarlos7121/4138840 to your computer and use it in GitHub Desktop.
WorkerService inmorible
package directorio.actividades.test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.widget.Toast;
public class WorkService extends IntentService{
public WorkService() {
super("WorkService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
int result = Activity.RESULT_CANCELED;
File file = null;
try{
URL url = new URL("http://admin.eldirectorio.mx/dbHandler.axd?SqliteDbVersion=338&nv=1&device=12");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
File SDCardRoot = Environment.getExternalStorageDirectory();
file = new File(SDCardRoot, "Volta.db");
System.out.println("Tama�o del archivo... " + urlConnection.getContentLength());
InputStream stream = urlConnection.getInputStream();
stream = new GZIPInputStream(stream);
InputSource is = new InputSource(stream);
InputStream input = new BufferedInputStream(is.getByteStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[2097152];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
result = Activity.RESULT_OK;
String location = file.getAbsolutePath();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bundle extras = intent.getExtras();
if (extras != null) {
Messenger messenger = (Messenger) extras.get("MESSENGER");
Message msg = Message.obtain();
msg.arg1 = result;
msg.obj = file.getAbsolutePath();
try {
messenger.send(msg);
} catch (android.os.RemoteException e1) {
Log.w(getClass().getName(), "Exception sending message", e1);
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return super.onBind(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment