在网络可用时发送电子邮件

时间:2013-12-24 11:55:25

标签: android email broadcastreceiver

我正在开发一个Android应用程序,用于安排在指定时间发送的电子邮件 现在,我通过设置广播接收器并使用未决意图来成功 现在的问题是:假设在预定的网络或互联网连接不可用,我该如何实现这一行动? 我可以为互联网连接注册广播接收器,但我不知道如何使用它 帮帮我。

每当用户设置时间我调用此setAlarm方法()

   private void setAlarm(Calendar targetCal) {

         email = editTextEmail.getText().toString();
         subject = editTextSubject.getText().toString();
         message = editTextMessage.getText().toString();
        //
        Toast.makeText(AlarmActivity.this, "Mail Scheduled at " + targetCal.getTime(),
                Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getBaseContext(), AlarmReciever.class);
        int uniqueValue = (int) System.currentTimeMillis();
        intent.putExtra("email", email);
        intent.putExtra("sub", subject);
        intent.putExtra("msg", message);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), uniqueValue, intent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
                pendingIntent);
        AlarmActivity.this.finish();

    }

在onReceive方法中,我将调用send email method()

  public static void sendMail(Context c,String em, String sub,String msg){
    try {

        sender.sendMail(sub, msg, un, em);
        Toast.makeText(c, "Mail Being Sent",Toast.LENGTH_LONG).show();
       no.flags=Notification.FLAG_AUTO_CANCEL; 
        nf.notify(Notify,no);

  } catch (Throwable t) {
        Toast.makeText(c,
                      "There are problem with sending mail. ",
                      Toast.LENGTH_LONG).show();
        mgr.notify(fail, note);

  }
}

3 个答案:

答案 0 :(得分:3)

android.net.conn.CONNECTIVITY_CHANGE

android.net.wifi.WIFI_STATE_CHANGED

每当网络状态发生变化,即连接或断开连接时,您将获得这两个广播。您可以接收这些广播并发送电子邮件。

有关详细信息,请参阅本教程

http://viralpatel.net/blogs/android-internet-connection-status-network-change/

答案 1 :(得分:1)

您可以直接打开“发送电子邮件”对话框发送电子邮件:

Intent emailActivity = new Intent(Intent.ACTION_SEND);
            emailActivity.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
            emailActivity.putExtra(Intent.EXTRA_SUBJECT, "SubjectName");
            emailActivity.setType("message/rfc822");
            startActivity(Intent.createChooser(emailActivity,
                    "Select your Email Provider :"));

如果您有任何疑惑,请参阅this demo

如果您想检查互联网连接,这很容易:

public class Internetconnection 
{
     public static boolean CheckNetConnectivity(Context mContext) 
     {
        ConnectivityManager connec = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
                || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)
            return true;

        return false;

    }
}

答案 2 :(得分:1)

  

使用此代码

Intent email = new Intent(Intent.ACTION_SEND);
   email.putExtra(Intent.EXTRA_EMAIL, new String[] { email address whom you want to send});
   email.putExtra(Intent.EXTRA_CC, new String[]{ to});
   email.putExtra(Intent.EXTRA_BCC, new String[]{to});
   email.setData(Uri.parse( email address whom you want to send)); 
   email.putExtra(Intent.EXTRA_SUBJECT, "Regards");
   startActivity(Intent.createChooser(emailActivity,
                        "Select your Email Provider :"));