Last active
October 27, 2016 22:58
-
-
Save marcherdiego/9e01b0000b618e66030fce897376d360 to your computer and use it in GitHub Desktop.
Taller parte 4: Mock de invocación de servicio con AsyncTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Button | |
android:text="Enviar" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/imageView" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentEnd="true" | |
android:id="@+id/button3" | |
android:onClick="enviarFoto"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/button3" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentEnd="true" | |
android:id="@+id/progress"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.AsyncTask; | |
import android.widget.TextView; | |
public class CameraActivity extends AppCompatActivity { | |
... | |
private TextView progress; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
... | |
progress = (TextView) findViewById(R.id.progress); | |
} | |
public void enviarFoto(View view) { | |
AsyncTask<Void, Integer, Boolean> imageSender = new AsyncTask<Void, Integer, Boolean>() { | |
private static final int ITERACIONES_TOTAL = 100; | |
@Override | |
protected Boolean doInBackground(Void... params) { | |
try { | |
for (int i = 0; i < ITERACIONES_TOTAL; ++i) { | |
publishProgress(i * 100 / ITERACIONES_TOTAL); | |
Thread.sleep(50); | |
} | |
return true; | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
@Override | |
protected void onProgressUpdate(Integer... values) { | |
progress.setText("Completado " + values[0] + "%"); | |
} | |
@Override | |
protected void onPostExecute(Boolean exito) { | |
if (exito) { | |
progress.setText("Envío finalizado con éxito"); | |
imageView.setImageResource(android.R.color.darker_gray); | |
} else { | |
progress.setText("Envío finalizado con error"); | |
} | |
} | |
}; | |
imageSender.execute(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest ... > | |
... | |
<uses-permission android:name="android.permission.INTERNET"/> | |
... | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment