PDA

Ver la Versión Completa : Toast en Intent


portal47
22/05/15, 00:36:01
Hola, estoy haciendo una app en la que si no hay Internet mando a abrir otro .class pero quiero que también aparezca un toast avisando que es porque no hay internet.

Aqui es donde mando el Intent:
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions.getJSONfromURL("http://cmx.org.mx/wp-content/uploads/json.txt");

if(jsonobject==null){
Intent intent = new Intent(MainActivity.this, CMX.class);
startActivity(intent);
finish();

}else{

try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("posts");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("title", jsonobject.getString("title"));
map.put("place", jsonobject.getString("place"));
map.put("date", jsonobject.getString("date"));
map.put("content1", jsonobject.getString("content1"));
map.put("content2", jsonobject.getString("content2"));
map.put("imagen", jsonobject.getString("imagen"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
}
return null;
}

ya intente poniendo el toast antes y despues del el intent y no aparece nada

joselehhh
22/05/15, 11:48:58
Si quieres actualizar la UI tienes que mostrar el toast en el onPostExecute. Devuelve en el doInBackground en vez de null, un valor x dependiendo de si has ejecutado o no el intent y en el onPostExecute usa ese valor para lanzas el toast diciendo si se ha ejecutado o no el intent. Corregidme si me equivoco.

Un saludo!

kriogeN
22/05/15, 12:14:40
Si quieres actualizar la UI tienes que mostrar el toast en el onPostExecute. Devuelve en el doInBackground en vez de null, un valor x dependiendo de si has ejecutado o no el intent y en el onPostExecute usa ese valor para lanzas el toast diciendo si se ha ejecutado o no el intent. Corregidme si me equivoco.

Un saludo!

Creo que los Toast los puedes ejecutar donde quieras, porque son llamadas al sistema independientes de la UI. Puedes "encolar" 20 Toast y después salir de la app y se mostrarán los 20 Toast encima de cualquier aplicación que vayas ejecutando.

mocelet
22/05/15, 14:04:37
El toast puedes llamarlo desde donde quieras sí. A mí muchas veces se me olvida llamar al método show() y me quedo un rato mosca hasta que me doy cuenta... lo comento por si acaso ;)

portal47
22/05/15, 16:10:06
Lo puse aqui y si funciona, justo debajo del doInBackground:


@ Override
protected void onPostExecute(Void args) {
// Lo cate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
if(jsonobject==null){
Toast.makeText(getApplicationContext(), "No hay conexion a internet!", Toast.LENGTH_SHORT).show();
}
}

Muchas gracias