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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| import android.annotation.TargetApi;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Timer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.lang.reflect.Array;
public class Majnotif extends Service {
int n = -1;
int compteur = 0;
OutputStreamWriter writer = null;
BufferedReader reader = null;
HttpURLConnection connexion;
String ligne = null;
Array primaire = null;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
private final static String name = "compteur";
@Override
public void onCreate() {
while (n < 0) {
try {
Thread.sleep(20000);
createNotification();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
// this.binder = null;
//this.timer.cancel();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public final void createNotification(){
try {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
try {
String cookies = android.webkit.CookieManager.getInstance().getCookie("http://2oclick.com/");
Log.d("NetworkState", "Le cookie est : " + cookies);
// Encodage des paramètres de la requête
if (cookies != null) {
String donnees = "?" + URLEncoder.encode("id", "UTF-8") + "=" + URLEncoder.encode("valeur1", "UTF-8");
//donnees += "&" + URLEncoder.encode("identifiant2", "UTF-8") + "=" + URLEncoder.encode("valeur2", "UTF-8");
// On a envoyé les données à une adresse distante
URL url = new URL("http://2oclick.com" + donnees);
connexion = (HttpURLConnection) url.openConnection();
connexion.setDoOutput(true);
connexion.setChunkedStreamingMode(0);
/* On envoie la requête ici
writer = new OutputStreamWriter(connexion.getOutputStream());
// On insère les données dans notre flux
writer.write(donnees);
// Et on s'assure que le flux est vidé
writer.flush();
*/
// On lit la réponse ici
reader = new BufferedReader(new InputStreamReader(connexion.getInputStream()));
// Tant que « ligne » n'est pas null, c'est que le flux n'a pas terminé d'envoyer des informations
while ((ligne = reader.readLine()) != null) {
Log.d("resultat", ligne);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
/*try {
writer.close();
} catch (Exception e) {
}*/
try {
reader.close();
} catch (Exception e) {
}
try {
connexion.disconnect();
} catch (Exception e) {
}
}
if (ligne != null) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.notification);
mBuilder.setContentTitle("Titre de la notification");
mBuilder.setContentText("Texte de la notification");
mBuilder.setAutoCancel(true);
mBuilder.setVibrate(new long[]{1000, 1000});
mBuilder.setLights(Color.WHITE, 1000, 1000);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
compteur,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//mNotificationManager.setAutoCancel();
// mId allows you to update the notification later on.
//mNotificationManager.ledARGB = color.RED;
mNotificationManager.notify(compteur, mBuilder.build());
compteur++;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
} |
Partager