Android Service start when push notification send and stop after service task complete

后端 未结 1 1398
情歌与酒
情歌与酒 2021-01-15 22:42

i want to start service when i send push notification through parse.com and when task of service ( backgrounds work) complete i stop the service. EDIT : in my service for s

相关标签:
1条回答
  • 2021-01-15 23:29

    Yes sure you can see the example

        public class MyService extends IntentService {
    
        public MyService(String name) {
            super("");
        }
    
        @Override
        protected void onHandleIntent(Intent arg0) {
            sendEmail();
        }
    
        public void sendEmail() {
            try {
                String host = "smtp.gmail.com";
                String address = "biraj@gmail.com";
                String from = "biraj@gmial.com";
                String pass = "biraj123";
                String to = "akash@gmail.com";
    
                Multipart multiPart;
                String finalString = "";
    
                Properties props = System.getProperties();
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", host);
                props.put("mail.smtp.user", address);
                props.put("mail.smtp.password", pass);
                props.put("mail.smtp.port", "587");
                props.put("mail.smtp.auth", "true");
                props.setProperty("mail.store.protocol", "imaps");
                Log.i("Check", "done pops");
                Session session = Session.getDefaultInstance(props, null);
                Store store = session.getStore();
                store.connect(host, address, pass);
    
                DataHandler handler = new DataHandler(new ByteArrayDataSource(finalString.getBytes(), "text/plain"));
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.setDataHandler(handler);
                Log.i("Check", "done sessions");
                multiPart = new MimeMultipart();
                InternetAddress toAddress;
                toAddress = new InternetAddress(to);
                message.addRecipient(Message.RecipientType.TO, toAddress);
                Log.i("Check", "added recipient");
                message.setSubject("Send Auto-Mail");
                message.setContent(multiPart);
                message.setText("Demo For Sending Mail in Android Automatically");
                Log.i("check", "transport");
                Transport transport = session.getTransport("smtp");
                Log.i("check", "connecting");
                transport.connect(host, address, pass);
                Log.i("check", "wana send");
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
                Log.i("check", "sent");
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    NOTE:

    No need to make separate thread or asynctask. Put the code directly that you want to execute in onHandleIntent() method.

    Service will stop itself as task gets completed.

    0 讨论(0)
提交回复
热议问题