Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
- import static mesutdokumaci.reader.Delete_Mail.delete;
- /**
- * Created by mesutdokumaci on 8.01.16.
- */
- public class DATABASE_KAYIT_SERVICE extends Service {
- Timer timing;
- Handler helper;
- int id = 1;
- String server, kullanıcı, sifre;
- public static String deneme = null;
- public static String govde;
- public static String tarih;
- public static String konu;
- public static String okunmadı;
- public static String connect = null;
- public static String ses2;
- public static Integer a;
- private static final int NOT_ID = 1;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- DATABASE_SERVER db = new DATABASE_SERVER(getApplicationContext());
- final HashMap<String, String> map = db.kullanici_detay(id);
- String time = map.get("zaman").toString();
- a = Integer.parseInt(time);
- System.out.println("ZAMAAANNN" + time);
- timing = new Timer();
- helper = new Handler(Looper.getMainLooper());
- timing.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- callMethod();
- }
- private void callMethod() {
- helper.post(new Runnable() {
- public void run() {
- DATABASE_SERVER asd = new DATABASE_SERVER(getApplicationContext());
- HashMap<String, String> map = asd.kullanici_detay(id);
- server = (map.get("mail_server").toString());
- kullanıcı = (map.get("kullanici").toString());
- sifre = (map.get("sifre").toString());
- System.out.println(server);
- System.out.println(kullanıcı);
- System.out.println(sifre);
- String host = server;
- String mailStoreType = "pop3";
- String username = kullanıcı;
- String password = sifre;
- String s = "açık";
- if (s.equals(map.get("kontrol").toString())) {
- Fetching_Mail(host, mailStoreType, username, password);
- delete(host, mailStoreType, username, password);
- } else {
- System.out.println("Otomatik Güncelleme Servisi Kapalıdır");
- }
- }
- protected void Fetching_Mail(String pop3Host, String storeType, String user, String password) {
- try {
- Properties properties = new Properties();
- properties.put("mail.store.protocol", "pop3");
- properties.put("mail.pop3.host", pop3Host);
- properties.put("mail.pop3.port", "110");
- properties.put("mail.pop3.starttls.enable", "false");
- Session emailSession = Session.getDefaultInstance(properties);
- Store store = emailSession.getStore("pop3");
- store.connect(pop3Host, user, password);
- connect = String.valueOf("a");
- Folder emailFolder = store.getFolder("INBOX");
- emailFolder.open(Folder.READ_ONLY);
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- System.in));
- Message[] messages = emailFolder.getMessages();
- System.out.println("messages.length---" + messages.length);
- deneme = String.valueOf(messages.length);
- for (int i = 0; i < messages.length; i++) {
- Message message = messages[i];
- System.out.println("---------------------------------");
- writePart(message);
- String line = reader.readLine();
- if ("YES".equals(line)) {
- message.writeTo(System.out);
- } else if ("QUIT".equals(line)) {
- break;
- }
- DATABASE_MAIL db = new DATABASE_MAIL(getApplicationContext());
- db.mail_ekle(tarih, konu, govde, null);
- db.close();
- }
- emailFolder.close(true);
- store.close();
- } catch (NoSuchProviderException e) {
- e.printStackTrace();
- } catch (MessagingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void writePart(Part p) throws Exception {
- if (p instanceof Message)
- writeEnvelope((Message) p);
- System.out.println("----------------------------");
- System.out.println("CONTENT-TYPE: " + p.getContentType());
- if (p.isMimeType("text/plain")) {
- System.out.println("This is plain text");
- System.out.println("---------------------------");
- System.out.println((String) p.getContent());
- govde = String.valueOf(p.getContent());
- System.out.println("Mail Veri Tabanına Eklendi");
- DATABASE_SERVER db = new DATABASE_SERVER(getApplicationContext());
- HashMap<String, String> map = db.kullanici_detay(id);
- String ses = map.get("ses");
- System.out.println("sesssss : " + ses);
- ses2 = "açık";
- System.out.println("Stringg sesssss : " + ses2);
- if (ses.equals(ses2)) {
- System.out.println("sesssss111 : " + ses);
- System.out.println("Stringg sesssss1111 : " + ses2);
- String ns = Context.NOTIFICATION_SERVICE;
- /*
- *BU ise 10 tane bildirim gelse bile üst üste güncelliyor 1 tane bildirim varmış gibi gösteriyor en son
- *geleni görüyoruz. tıkladığımızda en son gelenin ayrıntıları gösteriyor
- */
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
- int icon = R.mipmap.ic_launcher;
- CharSequence tickerText = "Bildirim ";
- long when = System.currentTimeMillis();
- int requestID = (int) System.currentTimeMillis();
- Notification notification = new Notification(icon, tickerText, when);
- Context context = getApplicationContext();
- Intent notificationIntent = new Intent(getApplicationContext(), Notifications.class);
- notificationIntent.putExtra("data1", tarih);
- notificationIntent.putExtra("data2", konu);
- notificationIntent.putExtra("data3", govde);
- notificationIntent.setAction("Bildirim" + requestID);
- // PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), requestID,new Intent(getApplication(),
- // Notifications.class), PendingIntent.FLAG_ONE_SHOT);
- PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), requestID, notificationIntent, 0);
- notificationIntent.setData((Uri.parse("mystring" + requestID++)));
- notification.setLatestEventInfo(context, konu, govde, contentIntent);
- notification.flags += Notification.FLAG_ONGOING_EVENT;
- notification.flags += Notification.FLAG_AUTO_CANCEL;
- mNotificationManager.notify(NOT_ID, notification);
- //Bu benim yapmış olduğum bildirimler tek tek geliyor tıkladığında ana sayfa açılıyor
- /* NotificationManager bildirimyoneticisi = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- Notification notification = new Notification(R.mipmap.ic_launcher, "BİLDİRİM", System.currentTimeMillis());
- Intent notificationIntent = new Intent(getApplicationContext(), mail_detay.class);
- // notificationIntent.putExtra("tarih",tarih);
- // notificationIntent.putExtra("konu",konu);
- notificationIntent.putExtra("id", mail_id);
- PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplication(),
- MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
- //) Intent intent = new Intent(getApplicationContext(), mail_detay.class);
- //intent.putExtra("id", mail_id);
- notification.setLatestEventInfo(getApplication(), konu, govde, pendingIntent);
- notification.flags = Notification.FLAG_AUTO_CANCEL;
- notification.defaults |= Notification.DEFAULT_VIBRATE;
- notification.defaults |= Notification.DEFAULT_SOUND;
- bildirimyoneticisi.notify((int) System.currentTimeMillis(), notification);
- */
- Fetching_Mail.tarih = " ";
- } else {
- System.out.println("sesssss2222 : " + ses);
- System.out.println("Stringg sesssss2222 : " + ses2);
- NotificationManager bildirimyoneticisi = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- Notification notification = new Notification(R.mipmap.ic_launcher, "BİLDİRİM", System.currentTimeMillis());
- PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplication(),
- MainActivity.class), PendingIntent.FLAG_ONE_SHOT);
- notification.setLatestEventInfo(getApplication(), konu, govde, pendingIntent);
- notification.flags = Notification.FLAG_AUTO_CANCEL;
- // notification.defaults |= Notification.DEFAULT_VIBRATE;
- // notification.defaults |= Notification.DEFAULT_SOUND;
- bildirimyoneticisi.notify((int) System.currentTimeMillis(), notification);
- System.out.println("Mail Silinmiştir");
- Fetching_Mail.tarih = " ";
- }
- }
- else if(p.isMimeType("multipart/*"))
- {
- System.out.println("This is a Multipart");
- System.out.println("---------------------------");
- Multipart mp = (Multipart) p.getContent();
- int count = mp.getCount();
- for (int i = 0; i < count; i++)
- writePart(mp.getBodyPart(i));
- }
- else if(p.isMimeType("message/rfc822"))
- {
- System.out.println("This is a Nested Message");
- System.out.println("---------------------------");
- writePart((Part) p.getContent());
- }
- else if(p.isMimeType("image/jpeg"))
- {
- System.out.println("--------> image/jpeg");
- Object o = p.getContent();
- InputStream x = (InputStream) o;
- // Construct the required byte array
- System.out.println("x.length = " + x.available());
- int i = 0;
- byte[] bArray = new byte[x.available()];
- while ((i = (int) ((InputStream) x).available()) > 0) {
- int result = (int) (((InputStream) x).read(bArray));
- if (result == -1)
- break;
- }
- FileOutputStream f2 = new FileOutputStream("/tmp/image.jpg");
- f2.write(bArray);
- }
- else if(p.getContentType().
- contains("image/")
- )
- {
- System.out.println("content type" + p.getContentType());
- File f = new File("image" + new Date(0).getTime() + ".jpg");
- DataOutputStream output = new DataOutputStream(
- new BufferedOutputStream(new FileOutputStream(f)));
- com.sun.mail.util.BASE64DecoderStream test =
- (com.sun.mail.util.BASE64DecoderStream) p
- .getContent();
- byte[] buffer = new byte[1024];
- int bytesRead;
- while ((bytesRead = test.read(buffer)) != -1) {
- output.write(buffer, 0, bytesRead);
- }
- }
- else
- {
- Object o = p.getContent();
- if (o instanceof String) {
- System.out.println("This is a string");
- System.out.println("---------------------------");
- System.out.println((String) o);
- } else if (o instanceof InputStream) {
- System.out.println("This is just an input stream");
- System.out.println("---------------------------");
- InputStream is = (InputStream) o;
- is = (InputStream) o;
- int c;
- while ((c = is.read()) != -1)
- System.out.write(c);
- } else {
- System.out.println("This is an unknown type");
- System.out.println("---------------------------");
- System.out.println(o.toString());
- }
- }
- }
- protected void writeEnvelope(Message m) throws Exception {
- System.out.println("This is the message envelope");
- System.out.println("---------------------------");
- Address[] a;
- if ((a = m.getFrom()) != null) {
- for (int j = 0; j < a.length; j++)
- System.out.println("FROM: " + a[j].toString());
- }
- if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
- for (int j = 0; j < a.length; j++)
- System.out.println("TO: " + a[j].toString());
- }
- if (m.getSubject() != null)
- System.out.println("SUBJECT: " + m.getSubject());
- konu = m.getSubject();
- if (m.getSentDate() != null)
- System.out.println("DATE: " + m.getSentDate());
- SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
- Locale.ENGLISH);
- Date parsedDate = sdf.parse(String.valueOf(m.getSentDate()));
- SimpleDateFormat print = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
- System.out.println(print.format(parsedDate));
- tarih = String.valueOf(print.format(parsedDate));
- System.out.println("YENİİ TARİHH" + tarih);
- }
- });
- }
- }, 0, a * 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement