Xasti
23/06/15, 13:10:04
Hola, y gracias de antemano, estoy desarrollando un sistema de seguridad para casa con una placa de arduino y lo controlo con una aplicación android que estoy haciendo yo también, ya he conseguido a través de la aplicación tener control de todos los sensores pero no soy capaz de hacer que mi aplicación me mande una notificación a la barra de tareas cuando se activa la alarma. Para ello lo que hago es convertir el html donde esta la información del estado del pin de alarma a un string y este compararle con el texto que deberia haber si la alarma esta activada, para ello uso este codigo:
public String convertirAstring() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://192.168.1.134/arduino/digital/13");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public void comprobar(){
String texto = convertirAstring();
String alarma = "Pin D13 set to 1";
if (texto.equals(alarma)) {
lanzamosNotificacion();
}
}
Y el codigo que tengo para la notificación es este:
public void LanzamosNotificacion(){
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence Text = "La alarma ha sido activada!!.";
CharSequence texto_1 = "Notificación de alarma";
CharSequence texto_titulo = "Alarma!!!!";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, texto_1, when);
Intent notificationIntent = new Intent(getApplicationContext()
,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationCont ext(), texto_titulo,
Text, contentIntent);
notificationManager.notify(1, notification);
}
Lo cierto es que no soy ningún experto, de hecho todo lo contrario y no soy capaz de hacer que funcione, he intentado hacerlo funcionar con AsyncTask pero no consigo que cuando se activa la alarma haga nada.
Si alguien me pudiera echar una mano estaría muy agradecido.
Un saludo.
public String convertirAstring() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://192.168.1.134/arduino/digital/13");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public void comprobar(){
String texto = convertirAstring();
String alarma = "Pin D13 set to 1";
if (texto.equals(alarma)) {
lanzamosNotificacion();
}
}
Y el codigo que tengo para la notificación es este:
public void LanzamosNotificacion(){
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence Text = "La alarma ha sido activada!!.";
CharSequence texto_1 = "Notificación de alarma";
CharSequence texto_titulo = "Alarma!!!!";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, texto_1, when);
Intent notificationIntent = new Intent(getApplicationContext()
,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationCont ext(), texto_titulo,
Text, contentIntent);
notificationManager.notify(1, notification);
}
Lo cierto es que no soy ningún experto, de hecho todo lo contrario y no soy capaz de hacer que funcione, he intentado hacerlo funcionar con AsyncTask pero no consigo que cuando se activa la alarma haga nada.
Si alguien me pudiera echar una mano estaría muy agradecido.
Un saludo.