2017-03-17 90 views
0

我目前正試圖讓AlarmReceiver在由DatePicker/TimePicker觸發時發送電子郵件。現在,這已經成功地發送推送通知到移動設備。我也知道SendMail方法在不同的項目中工作。JavaMail問題自動發送

這應該是工作,但我看不到觸發SendMail方法一旦AlarmReceiver類被調用的代碼的任何問題?

任何幫助,將不勝感激!謝謝!

Alarm.java

package com.example.pc.achieve; 

import java.util.Calendar; 

import android.content.DialogInterface; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v7.app.AlertDialog; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.Toast; 

public class Alarm extends Activity{ 

DatePicker pickerDate; 
TimePicker pickerTime; 
Button buttonSetAlarm; 
TextView info; 
private PendingIntent pendingIntent; 

final static int RQS_1 = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_alarm); 

    info = (TextView)findViewById(R.id.info); 
    pickerDate = (DatePicker)findViewById(R.id.pickerdate); 
    pickerTime = (TimePicker)findViewById(R.id.pickertime); 

    Calendar now = Calendar.getInstance(); 

    pickerDate.init(
      now.get(Calendar.YEAR), 
      now.get(Calendar.MONTH), 
      now.get(Calendar.DAY_OF_MONTH), 
      null); 

    pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY)); 
    pickerTime.setCurrentMinute(now.get(Calendar.MINUTE)); 

    buttonSetAlarm = (Button)findViewById(R.id.setalarm); 
    buttonSetAlarm.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 
      Calendar current = Calendar.getInstance(); 

      Calendar cal = Calendar.getInstance(); 
      cal.set(pickerDate.getYear(), 
        pickerDate.getMonth(), 
        pickerDate.getDayOfMonth(), 
        pickerTime.getCurrentHour(), 
        pickerTime.getCurrentMinute(), 
        00); 

      if(cal.compareTo(current) <= 0){ 
       //The set Date/Time already passed 
       Toast.makeText(getApplicationContext(), 
         "Date/Time Has Already Passed!", 
         Toast.LENGTH_LONG).show(); 
      }else { 
       setAlarm(cal); 
      } 

     }}); 

} 


private void setAlarm(Calendar targetCal){ 

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); 
    final int _id = (int) System.currentTimeMillis(); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Your alarm for this deadline has now been set for " + targetCal.getTime()) 
      .setCancelable(false) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        finish(); 
       } 
      }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 
} 

回答

1

廣播接收機執行可能會很快。

更好的是你應該啓動一個發送電子郵件的意圖服務。

+0

感謝您的回覆。那麼這個執行可能太快了?當報警觸發時,我希望電子郵件是自動的,是不是可以通過這種方式來完成? OnClick適用於單獨項目中的此方法,並且電子郵件會立即發送。 – ZenoX

+0

我的意思是說,而不是使用AsyncTask,您可以使用將在後臺進程中運行的Intent服務,但AsyncTask可能不會。 – albeee

+0

啊,我看到了,我還是新來的java和不確定如何更改此方法使用意圖服務,而不是AsyncTask?感謝您的幫助! – ZenoX