1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
package com.example.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import com.example.f_s.MainActivity;
import com.example.f_s.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class BackgroundService extends Service{
Context context;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate() {
super.onCreate();
start();
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void start(){
OutputStream sortieSocket;
PrintStream bw;
KeyStore trustStore;
BufferedReader br;
String str = "";
try {
// Configuration de la socket pour accepter le ceritificat auto signé
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// Création de la socket
Socket so = sf.createSocket();
SocketAddress soA = new InetSocketAddress("192.168.1.90" , 8098);
// Connection au serveur
so.connect(soA);
// Récupération des flux sur la socket
sortieSocket = so.getOutputStream();
bw = new PrintStream(sortieSocket);
br = new BufferedReader(new InputStreamReader(so.getInputStream()));
// Envoie des identifiants au serveur
bw.print("zefzefzfe");
bw.flush();
// Lecture des données du serveur
while(str != null){
str = br.readLine();
System.out.println("Réponse : "+str);
createNotification();
}
// Fermeture des ressources
bw.close();
br.close();
so.close();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
}
}
public void createNotification(){
NotificationManager notificationManager = null;
try {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
} catch (Exception e) {
System.out.println(e.toString());
}
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Pet Parrot";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Hungry!";
CharSequence contentText = "your parrot food meter is: ";
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(1, notification);
}
} |
Partager