设置应用程序以在达到时间设置时发送SMS MESSAGE

时间:2015-10-03 22:47:35

标签: java android sms alarmmanager smsmanager

用户设置日期和时间(都是editText),当确切的时间到达时,应用程序会拉出时间和日期并将其与当前时间进行比较,然后,它应该发送文本,但是当时间到了,没有任何反应。 这是我的代码的片段:

Calendar today=Calendar.getInstance();
Calendar alarmdate=Calendar.getInstance();
SimpleDateFormat dateFormatter;
String a,b;
Date duedate;

dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
String hour= String.valueOf(today.get(Calendar.HOUR_OF_DAY));
    String minute= String.valueOf(today.get(Calendar.MINUTE));
    String todaytime=hour+ " : " + minute;
    String alarmtime=tvtime.getText().toString();

try {
        duedate=dateFormatter.parse(tvdate.getText().toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    alarmdate.setTime(duedate);

a=dateFormatter.format(today.getTime());
    b=dateFormatter.format(alarmdate.getTime());
    if(today.equals(alarmdate)){
        if (todaytime.equals(alarmtime)){
            sendSMS(numbertotext,message);
        }
    }
}

private void sendSMS(String number, String message) {
    PendingIntent pi=PendingIntent.getActivity(this,0,new Intent(),0);
    SmsManager sms=SmsManager.getDefault();
    sms.sendTextMessage(number,null,message,pi,null);
}

我正在考虑使用alarmmanager但是它会如何工作? SMS代码是否在Alarm.java中?如何?请任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

  1. 创建AlarmReceiver 需要许可:

    公共类AlarmReceiver扩展了BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
      //send sms from here
    }
    

    }

  2. 设置闹钟

    AlarmManager报警;             待定内容;             Intent otherIntent = new Intent();             otherIntent.setClass(this,AlarmReceiver.class);             alarm =(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);             intent = PendingIntent.getService(这个,                     (int)System.currentTimeMillis(),otherIntent,                     PendingIntent.FLAG_UPDATE_CURRENT);             alarm.set(AlarmManager.RTC_WAKEUP,SCHEDULED_TIME,intent);

  3. 将所有预定消息存储在数据库中,并从AlarmReceiver中,从预定时间< =当前时间的数据库中获取所有消息。 您可能需要的另一件事是在发生警报时手机关闭时处理这种情况,在这种情况下,启动服务时启动并发送所有待处理的消息。

    有关警报示例,请参阅:https://github.com/AppLozic/Applozic-Android-SDK/blob/master/mobicomkit/src/main/java/com/applozic/mobicomkit/api/conversation/schedule/ScheduledMessageUtil.java

相关问题