ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

推送通知发送时Android服务启动,服务任务完成后停止

2019-10-09 21:25:54  阅读:125  来源: 互联网

标签:parse-platform android push-notification service


我想通过parse.com发送推送通知时启动服务,并且当服务任务(后台工作)完成时我停止服务.
编辑:在我的服务中发送邮件

 try {
                sender = new GMailSender("id",
                        "password");

                sender.sendMail(
                        "subject",
                        "This is Body", "sender mail id",
                        "recei. id");


            } catch (Exception e) {
                Log.e("SendMail", e.getMessage(), e);
            }

Gmail发件人类别

  public class GMailSender extends javax.mail.Authenticator{
  //public class GMailSender extends javax.mail.Authenticator {   
private String mailhost ="smtp.gmail.com";   
private String user;   
private String password;   
private Session session;  
private Multipart multipart = new MimeMultipart();
MimeMessage message;

static {   
    Security.addProvider(new com.provider.JSSEProvider());   
}  

public GMailSender(String user, String password) {   
    this.user = user;   
    this.password = password;   

    Properties props = new Properties();   
    props.setProperty("mail.transport.protocol", "smtp");   
    props.setProperty("mail.host", mailhost);   
    props.put("mail.smtp.auth", "true");   
    props.put("mail.smtp.port", "465");   
    props.put("mail.smtp.socketFactory.port", "465");   
    props.put("mail.smtp.socketFactory.class",   
            "javax.net.ssl.SSLSocketFactory");   
    props.put("mail.smtp.socketFactory.fallback", "false");   
    props.setProperty("mail.smtp.quitwait", "false");   

    session = Session.getDefaultInstance(props, this);   
}   

protected PasswordAuthentication getPasswordAuthentication() {   
    return new PasswordAuthentication(user, password);   
}   

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
    try{
    message = new MimeMessage(session);   
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
    message.setSender(new InternetAddress(sender));   
    message.setSubject(subject);   
    message.setDataHandler(handler); 


    if (recipients.indexOf(',') > 0)   
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
    else  
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
    Transport.send(message);   
    }catch(Exception e){

    }
    Transport.send(message);
}   

    public void addAttachment(String filename) throws Exception { 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(filename); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(filename); 

        multipart.addBodyPart(messageBodyPart); 
        message.setContent(multipart);
        System.out.println("u r in add attachment"+multipart);
      } 

//}
//
    公共类ByteArrayDataSource实现DataSource {
        私有字节[]数据;
        私有字符串类型;

    public ByteArrayDataSource(byte[] data, String type) {   
        super();   
        this.data = data;   
        this.type = type;   
    }   

    public ByteArrayDataSource(byte[] data) {   
        super();   
        this.data = data;   
    }   

    public void setType(String type) {   
        this.type = type;   
    }   

    public String getContentType() {   
        if (type == null)   
            return "application/octet-stream";   
        else  
            return type;   
    }   

    public InputStream getInputStream() throws IOException {   
        return new ByteArrayInputStream(data);   
    }   

    public String getName() {   
        return "ByteArrayDataSource";   
    }   

    public OutputStream getOutputStream() throws IOException {   
        throw new IOException("Not Supported");   
    }   
}   

}

JSSE课

    public class JSSEProvider extends Provider {
public JSSEProvider() {
    super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
    AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
        public Void run() {
            put("SSLContext.TLS",
                    "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
            put("Alg.Alias.SSLContext.TLSv1", "TLS");
            put("KeyManagerFactory.X509",
                    "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
            put("TrustManagerFactory.X509",
                    "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
            return null;
        }
    });
}

}

解决方法:

是的,您可以看到示例

    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();
        }
    }
}

注意:

无需创建单独的线程或asynctask.将要执行的代码直接放在onHandleIntent()方法中.

服务将在任务完成时自行停止.

标签:parse-platform,android,push-notification,service
来源: https://codeday.me/bug/20191009/1882226.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有