你好朋友我是android新手。我正在开发一个Daily Report的应用程序。在这个应用程序中,我想每24小时自动向用户发送一封电子邮件。我发现如何在没有Intent的情况下使用我的应用程序发送电子邮件。但我无法获得如何每24小时发送一封电子邮件的解决方案,请帮助我..
public class SendAttachment
{
public static void main()
{
String to="emailaddress";
final String user="emailaddress";//change accordingly
final String password="password";//change accordingly
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
System.out.println("111111111111111111111111111111111111111111111111");
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Properties properties = System.getProperties();
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
javax.mail.Session session=javax.mail.Session.getDefaultInstance(properties,new Authenticator()
{
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user, password);
}
});
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("text");
//3) create MimeBodyPart object and set your message content
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("Daily ConstructionReport");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
//Location of file to be attached
String filename = Environment.getExternalStorageDirectory().getPath()+"/text/tedt_unu.pdf";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName("Report");
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart);
//7) send message
Transport.send(message);
System.out.println("MESSAGE SENT....");
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
}
}
当我想发送电子邮件时,这是我的发送附件类我刚刚调用方法SendAttachment.main()。现在我可以把这个方法放在这里,这样我就可以每隔24小时发一封电子邮件..
public class PollReceiver extends BroadcastReceiver
{
private static final int PERIOD=5000;
@Override
public void onReceive(Context ctxt, Intent i)
{
scheduleAlarms(ctxt);
}
static void scheduleAlarms(Context ctxt)
{
AlarmManagermgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, ScheduledService.class);
PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 5000,PERIOD, pi);
}
}
这是我的Intent服务类
public class ScheduledService extends IntentService
{
public ScheduledService()
{
super("ScheduledService");
}
@Override
protected void onHandleIntent(Intent intent)
{
SendAttachment.main();
Log.d(getClass().getSimpleName(), "I ran!");
}
这是我的主要活动
CheckBox ch1=(CheckBox)findViewById(R.id.checkBox1);
stopService(new Intent(getApplicationContext(), ScheduledService.class));
ch1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
PollReceiver.scheduleAlarms(ScheduledServiceDemoActivity.this);
}
else
{
}
});
答案 0 :(得分:0)
请看这个以激活警报:Android AlarmManager
这是发送电子邮件的代码:
Intent email = new Intent (Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ "EmailTarget@gmail.com"});
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
email.putExtra(Intent.EXTRA_SUBJECT,"Your Subject ");
email.putExtra(Intent.EXTRA_TEXT, "Your Text Message");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client:"));
希望这是有帮助的。
答案 1 :(得分:0)
将AlarmManager与BroadcastReceiver结合使用,如下所示:
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent emailIntent = new Intent(context, EmailAlarmReceiver.class);
// use emailIntent.putExtra(key, value) to put extras like what email to send
PendingIntent emailAlarm = PendingIntent.getBroadcast(context, 0,
emailIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long oneDay = 24 * 60 * 60 * 1000;
alarmManager.setRepeating(AlarmManager.RTC,
System.currentTimeMillis() + oneDay, oneDay, emailAlarm);
BroadcastReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// get extras from intent and send email
}
}
不要忘记将它放在application-tag中的清单中:
<receiver android:name=".EmailAlarmReceiver" />
答案 2 :(得分:0)
创建服务,将电子邮件部分添加到其中,然后创建一个每天触发服务的AlarmManager。
你们都准备好了。
对于复选框部分:
如果您想在选中复选框后立即发送电子邮件,则需要执行以下操作:
checkBox = (CheckBox)findViewById(R.id.checkBox);
checkBox.setOnClickListener(this);
@Override
public void onClick(View v)
{
if(v.getId() == checkBox.getId())
{
if (checkBox.isChecked() == true)
{
//send email immediately
//add the reference to true to DB or sharedPreference
}
else
{
}
}
}
示例:强>
如果您想稍后通过服务发送电子邮件。
如果您已将参考存储在DB / Shared Preference中 - 获取值。
boolean checkBox = db.getCheckbox()
if (checkBox == true)
{
//sendEmail
}
else
{
//stopservice
}
停止服务:
如果您的活动:
Intent startServiceIntent = new Intent(this, EmailService.class);
this.stopService(startServiceIntent);
this.startService(startServiceIntent);
如果在服务范围内:
stopSelf();