Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package said.app.bpf;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Handler;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.sql.SQLOutput;
- import java.util.ArrayList;
- /**
- * Created by Said on 25/06/2016.
- */
- public class AlarmReceiver extends BroadcastReceiver {
- private Handler handler = new Handler();
- String jogadores;
- String Nick = "";
- ArrayList<String> lista = new ArrayList<>();
- @Override
- public void onReceive(Context context, Intent intent) {
- System.out.println("FOI");
- Ler(context);
- }
- public void Ler(final Context context)
- {
- new Thread(){
- public void run()
- {
- try{
- URL myurl = null;
- try {
- myurl = new URL("http://192.168.1.126/bpf/ler.php");
- }
- catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- URLConnection myconn = myurl.openConnection();
- InputStream in = new BufferedInputStream(myconn.getInputStream());
- InputStreamReader reader = new InputStreamReader(in);
- BufferedReader br = new BufferedReader(reader);
- String line;
- StringBuilder sb = new StringBuilder();
- while ((line=br.readLine()) != null)
- {
- sb.append(line);
- //Toast.makeText(getApplicationContext(), "I enter here", Toast.LENGTH_LONG).show();
- }
- System.out.println( sb.toString());
- jogadores= sb.toString();
- Toast.makeText(context,jogadores,Toast.LENGTH_LONG).show();
- }
- catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }catch (Exception e)
- {
- }
- handler.post(new Runnable() {
- @Override
- public void run() {
- try {
- lista = ParaArray(jogadores);
- ChecarDenuncia(context);
- } catch (JSONException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- }}.start();
- }
- ArrayList<String> ParaArray(String json) throws JSONException
- {
- ArrayList<String> lista = new ArrayList<String>();
- JSONArray jsonArray = new JSONArray(json);
- for (int i = 0; i < jsonArray.length(); i++) {
- lista.add(jsonArray.getString(i));
- }
- return lista;
- }
- void ChecarDenuncia(Context context) throws IOException, JSONException {
- System.out.println("CHECAR DENUNCIA");
- File arquivoa = new File(context.getFilesDir()+"/settings.ini");
- FileReader fra = new FileReader(arquivoa);
- BufferedReader lera = new BufferedReader(fra);
- Nick = lera.readLine();
- System.out.println("O NICK DO CHECAR DENUNCIA É :"+Nick);
- if (!Nick.equals("")) {
- File arquivo = new File(context.getFilesDir() + "/settings.ini");
- String Nick = "";
- int ndenuncias = 0;
- int DenunciaAntiga = 0;
- if (!arquivo.exists()) {
- arquivo.createNewFile();
- }
- FileReader fr = new FileReader(arquivo);
- BufferedReader ler = new BufferedReader(fr);
- for (int i = 0; i < 2; i++) {
- if (i == 0) {
- Nick = ler.readLine();
- }
- if (i == 1) {
- ndenuncias = Integer.parseInt(ler.readLine());
- }
- }
- DenunciaAntiga = ndenuncias;
- System.out.println("JOGADORRRR:" + Nick);
- System.out.println("NDENUNCIAS:" + ndenuncias);
- ler.close();
- fr.close();
- ArrayList<String> DenunciasJogador = new ArrayList<>();
- ndenuncias = 0;
- for (int i = 0; i < lista.size(); i++) {
- JSONArray jsonArray = new JSONArray(lista.get(i));
- DenunciasJogador.add(jsonArray.getString(0));
- // System.out.println(jsonArray.getString(0));
- if (jsonArray.getString(0).contains(Nick)) {
- ndenuncias++;
- }
- }
- if(ndenuncias > DenunciaAntiga)
- {
- System.out.println("HA UMA DENUNCIA NOVA ");
- Intent intent = new Intent();
- PendingIntent pIntent = PendingIntent.getActivity(context,0,intent,0);
- Notification noti = new Notification.Builder(context).
- setTicker("BPF Denúncia")
- .setContentTitle("BPF Denúncia")
- .setContentText("Há Uma Nova Denúncia Contra Você!")
- .setSmallIcon(R.drawable.ic_launcher)
- .setContentIntent(pIntent).getNotification();
- noti.flags = Notification.FLAG_AUTO_CANCEL;
- NotificationManager nm = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
- nm.notify(0,noti);
- }
- System.out.println("N DENUNCIA ANTIGA:"+DenunciaAntiga);
- System.out.println("DENUNCIA NOVA:"+ndenuncias);
- System.out.println("CHEGOU FINAL ANTES abrir arquivo ");
- File arquivo2 = new File(context.getFilesDir() + "/settings.ini");
- FileWriter fw = new FileWriter(arquivo2);
- BufferedWriter escrever = new BufferedWriter(fw);
- System.out.println("CHEGOU FINAL ANTES ESCREVER ");
- escrever.write(Nick);
- escrever.newLine();
- escrever.write(String.valueOf(ndenuncias));
- escrever.close();
- fw.close();
- ndenuncias = 0;
- DenunciaAntiga = 0;
- System.out.println("CHEGOU FINAL ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement