我正在开发一个安排要发送邮件的Android应用程序。我正在使用报警管理器。我的主要代码如下:
mConfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent =
new Intent(getApplicationContext(),MyNotificationService.class);
mMessage = mEditTextMessage.getText().toString();
mNumber = mEditTextNumber.getText().toString();
c.set(mYear, mMonth, mDay);
c.set(Calendar.HOUR_OF_DAY, mHour);
c.set(Calendar.MINUTE, mMinutes);
Bundle bundle = new Bundle();
bundle.putCharSequence("number", mNumber);
bundle.putCharSequence("message", mMessage);
intent.putExtras(bundle);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent =
PendingIntent.getService(getApplicationContext(), 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
pendingIntent);
}
});
我应该如何实现MyNotificationService类,以便它在设定的时间发送消息?该类应该是服务还是广播接收器?
答案 0 :(得分:2)
下面的代码将允许您每5秒钟调用一次广播接收器。您可以使用set()或setTimeZone()方法在特定时间设置。
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra("abc", Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 5 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi);
AlarmManagerBroadcastReceiver类是一个广播公司。
答案 1 :(得分:1)
您可以使用广播接收器接收来自意图的消息。 您可以创建一个接收器类,您可以在其中获取消息。
Intent intent=new Intent(MyReceiver.ACTION_REFRESH_ALARM);
pendingIntent=PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,0,pendingIntent);
public class MyReceiver extends BroadcastReceiver
{
public static final String ACTION_REFRESH_ALARM ="com.paad.network.ACTION_REFRESH_ALARM";
@Override
public void onReceive(Context context, Intent intent) {
//Extract Messages
}}