I need to make an app for android in which images with information of events are displayed. This I already have what I want to do is that when the app is in the background the Asynctask
is run every so often to know if a new image has been added and send a notification.
Here's the code:
public class MainEventos extends AppCompatActivity {
private
ListView listView;
ArrayList imagen = new ArrayList();
Bitmap photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_eventos);
listView = (ListView)findViewById(R.id.listview);
descargarImagen();
}
protected void onStop() {
super.onStop();
}
public void descargarImagen(){
imagen.clear();
final ProgressDialog progressDialog = new ProgressDialog(MainEventos.this);
progressDialog.setMessage("Cargando Imagenes");
progressDialog.show();
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.1.102/androidimg/wsblob.php", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode == 200){
progressDialog.dismiss();
try {
JSONArray jsonArray = new JSONArray(new String(responseBody));
for (int i=0;i<jsonArray.length();i++){
imagen.add(jsonArray.getJSONObject(i).getString("imagen"));
}
listView.setAdapter(new ImagenAdapter(getApplicationContext()));
}catch (JSONException e){
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
private class ImagenAdapter extends BaseAdapter{
Context ctx;
LayoutInflater layoutInflater;
ImageView imageView;
Bitmap photo;
public ImagenAdapter(Context applicationContext){
this.ctx=applicationContext;
layoutInflater = (LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imagen.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup viewGroup = (ViewGroup)layoutInflater.inflate(R.layout.activity_main_item, null);
imageView=(ImageView)viewGroup.findViewById(R.id.imagen1);
byte[] byteData = Base64.decode(imagen.get(position).toString(), Base64.DEFAULT);
photo = BitmapFactory.decodeByteArray( byteData, 0,
byteData.length);
imageView.setImageBitmap(photo);
return viewGroup;
}
}
}