Android:IntentService无法正常排队

Android:IntentService无法正常排队,第1张

概述我试图使用Intent Service在后台运行以发送带有相当大(1-2mb)图像附件的电子邮件.我是初学者,到目前为止通过网络研究已经学到了所有东西(所以你可能会看到一些类似的代码). 发生的事情是,当我发送第一封电子邮件时,一切正常.然而,当我在第一个仍在处理时尝试发送另一个时,第一个被丢弃而第二个被罚款.它将始终发送最新的排队意图并跳过之前的意图. 根据我所做的研究,IntentServic 我试图使用Intent Service在后台运行以发送带有相当大(1-2mb)图像附件的电子邮件.我是初学者,到目前为止通过网络研究已经学到了所有东西(所以你可能会看到一些类似的代码).

发生的事情是,当我发送第一封电子邮件时,一切正常.然而,当我在第一个仍在处理时尝试发送另一个时,第一个被丢弃而第二个被罚款.它将始终发送最新的排队意图并跳过之前的意图.

根据我所做的研究,IntentService应该对传递给它的意图进行排队,并按顺序依次执行它们.这个问题:IntentService : How to enqueue correctly?是类似的,但我不认为它完全适用于我的情况.

我确信我遗失了一些愚蠢的东西=(

这是我启动服务的onActivityResult:

public voID onActivityResult(int requestCode,int resultCode,Intent data) {    Log.i(TAG,"Entered onActivityResult");    super.onActivityResult(requestCode,resultCode,data);    if (resultCode == RESulT_CANCELED) {        Log.i(TAG,"onActivityResult Canceled");        Toast toast = Toast.makeText(this,"Camera Canceled",10000);        toast.show();        return;    }    if ((requestCode == CAMERA_PIC_REQUEST) && (resultCode == Activity.RESulT_OK)) {        Log.i(TAG,"onActivityResult Result OK");        String filePath = getoutputPath();        String order = getorder();        Log.i(TAG,"Sending From: " + filePath);        Intent sendIntent = new Intent(this,MailintentService.class);        sendIntent.putExtra(MailintentService.IN_SUBJECT,order);        sendIntent.putExtra(MailintentService.IN_PATH,filePath);        startService(sendIntent);        Toast.makeText(this,"Image Upload Started.  Check Notification bar for Status.",Toast.LENGTH_LONG).show();    }}

这是整个IntentService:

public class MailintentService extends IntentService {//These set the parameters for the Emailpublic static final String destemail = "hIDden";public static final String sendEmail = "hIDden";public static final String sendPass = "hIDden";public static final String body = "Growerlive image upload request.";public static final String IN_SUBJECT = "insub";public static final String IN_PATH = "inpath";//This is the standard tag for class nameprivate static final String TAG = MailintentService.class.getSimplename();//These set up the parameters for notificationsprivate notificationmanager mnotificationmanager;private Notification notifyDetails;private int NOTIFICATION_ID;private CharSequence contentTitle = "Bethel Upload";private CharSequence contentText = "Image Uploading...";private String contentTicker = "Upload Started...";public voID onCreate() {    super.onCreate();    Log.i(TAG,"MailintentService Created");    mnotificationmanager = (notificationmanager)getSystemService(NOTIFICATION_SERVICE);    notifyDetails = new Notification(R.drawable.ic_launcher,contentTicker,System.currentTimeMillis());}public voID onDestroy() {    super.onDestroy();    file dir = new file(Environment.getExternalStorageDirectory() + "/BethelUpload");    if (dir.isDirectory()) {         String[] children = dir.List();         for (int i = 0; i < children.length; i++) {             new file(dir,children[i]).delete();         }     }    Log.i(TAG,"MailintentService Destroyed");}public MailintentService() {    super(TAG);}@OverrIDeprotected voID onHandleIntent(Intent intent) {    Context context = getApplicationContext();    Intent notifyIntent = new Intent(BethelUploadActivity.class.getSimplename());    PendingIntent intentP = PendingIntent.getActivity(MailintentService.this,notifyIntent,androID.content.Intent.FLAG_ACTIVITY_NEW_TASK);    notifyDetails.setLatestEventInfo(context,contentTitle,contentText,intentP);    try {        String subject = intent.getStringExtra(IN_SUBJECT);        String path = intent.getStringExtra(IN_PATH);        Log.i(TAG,"Sending Mail...");        Log.i(TAG,"Subject: " + subject);        Log.i(TAG,"Attachment Path: " + path);        mnotificationmanager.notify(NOTIFICATION_ID,notifyDetails);        GMailSender sender = new GMailSender(sendEmail,sendPass);         sender.sendMail(subject,body,sendEmail,destemail,path);        Log.i(TAG,"Mail Sent!");        contentText = "Image Uploaded Successfully";        contentTicker = "Upload Successful";        notifyDetails.setLatestEventInfo(context,intentP);        mnotificationmanager.notify(NOTIFICATION_ID,notifyDetails);    }     catch (Exception e) {            Log.e(TAG,e.getMessage(),e);        }       }}

这是我在另一个例子中使用的GMailSender:Sending Email in Android using JavaMail API without using the default/built-in app

public class GMailSender extends javax.mail.Authenticator {        private String mailhost = "smtp.gmail.com";        private String user;        private String password;        private Session session;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,String fileAttachment) throws Exception {    try{     MimeMessage message = new MimeMessage(session);        message.setSender(new InternetAddress(sender));        message.setSubject(subject);    MimeBodyPart messageBodyPart = new MimeBodyPart();    messageBodyPart.setText(body);    Multipart multipart = new MimeMultipart();    multipart.addBodyPart(messageBodyPart);    messageBodyPart = new MimeBodyPart();    DataSource source = new fileDataSource(fileAttachment);    messageBodyPart.setDataHandler(new DataHandler(source));    messageBodyPart.setfilename("SodPhoto.jpg");    multipart.addBodyPart(messageBodyPart);    message.setContent(multipart);    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){    }}public class ByteArrayDataSource implements DataSource {    private byte[] data;    private String type;    public ByteArrayDataSource(byte[] data,String type) {        @R_404_4733@;        this.data = data;        this.type = type;    }    public ByteArrayDataSource(byte[] data) {        @R_404_4733@;        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");    }}}

我知道这是一个庞大的代码/文本墙,但我想我宁愿给你太多的信息而不是太少.我非常感谢你的时间!

解决方法 我认为这将是解决方案……

GMailSender sender = new GMailSender(sendEmail,sendPass);          SystemClock.sleep(30000);    sender.sendMail(subject,path);

这使得您的第一个邮件在30秒内从当前系统时间延迟发送,第二个邮件也以这种方式发送…

总结

以上是内存溢出为你收集整理的Android:IntentService无法正常排队全部内容,希望文章能够帮你解决Android:IntentService无法正常排队所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1126477.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存