Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. package said.app.bpf;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Handler;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16.  
  17. import java.io.BufferedInputStream;
  18. import java.io.BufferedReader;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.FileWriter;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28. import java.net.URLConnection;
  29. import java.sql.SQLOutput;
  30. import java.util.ArrayList;
  31.  
  32. /**
  33. * Created by Said on 25/06/2016.
  34. */
  35. public class AlarmReceiver extends BroadcastReceiver {
  36.  
  37. private Handler handler = new Handler();
  38. String jogadores;
  39. String Nick = "";
  40. ArrayList<String> lista = new ArrayList<>();
  41.  
  42.  
  43. @Override
  44. public void onReceive(Context context, Intent intent) {
  45.  
  46. System.out.println("FOI");
  47.  
  48. Ler(context);
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. public void Ler(final Context context)
  59. {
  60.  
  61. new Thread(){
  62. public void run()
  63. {
  64.  
  65. try{
  66.  
  67.  
  68.  
  69.  
  70. URL myurl = null;
  71.  
  72. try {
  73. myurl = new URL("http://192.168.1.126/bpf/ler.php");
  74. }
  75. catch (MalformedURLException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. }
  79. try {
  80. URLConnection myconn = myurl.openConnection();
  81. InputStream in = new BufferedInputStream(myconn.getInputStream());
  82. InputStreamReader reader = new InputStreamReader(in);
  83. BufferedReader br = new BufferedReader(reader);
  84. String line;
  85. StringBuilder sb = new StringBuilder();
  86. while ((line=br.readLine()) != null)
  87. {
  88. sb.append(line);
  89. //Toast.makeText(getApplicationContext(), "I enter here", Toast.LENGTH_LONG).show();
  90. }
  91.  
  92. System.out.println( sb.toString());
  93. jogadores= sb.toString();
  94. Toast.makeText(context,jogadores,Toast.LENGTH_LONG).show();
  95.  
  96. }
  97. catch (IOException e) {
  98. // TODO Auto-generated catch block
  99. e.printStackTrace();
  100. }
  101.  
  102. }catch (Exception e)
  103. {
  104.  
  105. }
  106.  
  107.  
  108. handler.post(new Runnable() {
  109. @Override
  110. public void run() {
  111.  
  112.  
  113. try {
  114. lista = ParaArray(jogadores);
  115.  
  116.  
  117. ChecarDenuncia(context);
  118.  
  119. } catch (JSONException e) {
  120. e.printStackTrace();
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. });
  126. }}.start();
  127.  
  128.  
  129.  
  130.  
  131. }
  132.  
  133.  
  134. ArrayList<String> ParaArray(String json) throws JSONException
  135.  
  136. {
  137. ArrayList<String> lista = new ArrayList<String>();
  138. JSONArray jsonArray = new JSONArray(json);
  139. for (int i = 0; i < jsonArray.length(); i++) {
  140. lista.add(jsonArray.getString(i));
  141.  
  142. }
  143.  
  144. return lista;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. void ChecarDenuncia(Context context) throws IOException, JSONException {
  152.  
  153. System.out.println("CHECAR DENUNCIA");
  154. File arquivoa = new File(context.getFilesDir()+"/settings.ini");
  155. FileReader fra = new FileReader(arquivoa);
  156. BufferedReader lera = new BufferedReader(fra);
  157.  
  158.  
  159. Nick = lera.readLine();
  160.  
  161.  
  162. System.out.println("O NICK DO CHECAR DENUNCIA É :"+Nick);
  163. if (!Nick.equals("")) {
  164. File arquivo = new File(context.getFilesDir() + "/settings.ini");
  165. String Nick = "";
  166. int ndenuncias = 0;
  167. int DenunciaAntiga = 0;
  168. if (!arquivo.exists()) {
  169.  
  170. arquivo.createNewFile();
  171. }
  172.  
  173. FileReader fr = new FileReader(arquivo);
  174. BufferedReader ler = new BufferedReader(fr);
  175.  
  176. for (int i = 0; i < 2; i++) {
  177.  
  178.  
  179. if (i == 0) {
  180. Nick = ler.readLine();
  181. }
  182. if (i == 1) {
  183. ndenuncias = Integer.parseInt(ler.readLine());
  184. }
  185.  
  186. }
  187. DenunciaAntiga = ndenuncias;
  188.  
  189. System.out.println("JOGADORRRR:" + Nick);
  190. System.out.println("NDENUNCIAS:" + ndenuncias);
  191. ler.close();
  192. fr.close();
  193.  
  194.  
  195. ArrayList<String> DenunciasJogador = new ArrayList<>();
  196. ndenuncias = 0;
  197. for (int i = 0; i < lista.size(); i++) {
  198.  
  199. JSONArray jsonArray = new JSONArray(lista.get(i));
  200.  
  201. DenunciasJogador.add(jsonArray.getString(0));
  202. // System.out.println(jsonArray.getString(0));
  203.  
  204.  
  205. if (jsonArray.getString(0).contains(Nick)) {
  206.  
  207. ndenuncias++;
  208. }
  209. }
  210.  
  211. if(ndenuncias > DenunciaAntiga)
  212. {
  213. System.out.println("HA UMA DENUNCIA NOVA ");
  214.  
  215. Intent intent = new Intent();
  216. PendingIntent pIntent = PendingIntent.getActivity(context,0,intent,0);
  217.  
  218. Notification noti = new Notification.Builder(context).
  219. setTicker("BPF Denúncia")
  220. .setContentTitle("BPF Denúncia")
  221. .setContentText("Há Uma Nova Denúncia Contra Você!")
  222. .setSmallIcon(R.drawable.ic_launcher)
  223. .setContentIntent(pIntent).getNotification();
  224.  
  225. noti.flags = Notification.FLAG_AUTO_CANCEL;
  226. NotificationManager nm = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
  227. nm.notify(0,noti);
  228.  
  229.  
  230. }
  231. System.out.println("N DENUNCIA ANTIGA:"+DenunciaAntiga);
  232. System.out.println("DENUNCIA NOVA:"+ndenuncias);
  233. System.out.println("CHEGOU FINAL ANTES abrir arquivo ");
  234.  
  235. File arquivo2 = new File(context.getFilesDir() + "/settings.ini");
  236.  
  237.  
  238. FileWriter fw = new FileWriter(arquivo2);
  239.  
  240. BufferedWriter escrever = new BufferedWriter(fw);
  241. System.out.println("CHEGOU FINAL ANTES ESCREVER ");
  242. escrever.write(Nick);
  243. escrever.newLine();
  244. escrever.write(String.valueOf(ndenuncias));
  245.  
  246. escrever.close();
  247. fw.close();
  248.  
  249.  
  250. ndenuncias = 0;
  251. DenunciaAntiga = 0;
  252. System.out.println("CHEGOU FINAL ");
  253. }
  254.  
  255.  
  256.  
  257.  
  258.  
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement