android 自动推送,android – 在特定时间发送自动消息

我正在开发一个应用程序,我需要将短信发送到特定的电话号码.我可以使用以下代码发送短信.

try {

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage(phoneNo, null, sms, null, null);

Toast.makeText(getApplicationContext(), "SMS Sent!",

Toast.LENGTH_LONG).show();

} catch (Exception e) {

Toast.makeText(getApplicationContext(),

"SMS faild, please try again later!",

Toast.LENGTH_LONG).show();

e.printStackTrace();

}

现在我想要的是短信应该自动进行.消息应该自动进入的时间存储在MySQL数据库中.因此,我需要在该时间到来时继续检查的代码,然后自动将消息发送到该号码.它是一种提醒事物.用户将在应用程序中保留提醒,例如;我需要在1小时后收到消息.所以1小时后消息应该会出现. PLZ帮忙?

终于我明白了.

/** Code For reminder is here */

int time=Integer.parseInt(answer);

int num = (int)System.currentTimeMillis();

Intent intent = new Intent(getApplication(), MyBroadcastReceiver.class);

intent.putExtra("phoneNo",phoneNo);

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.MINUTE, time);

PendingIntent pendingIntent = PendingIntent.getBroadcast(

getApplicationContext(), num, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()

+ calendar.getTimeInMillis() , pendingIntent);

Toast.makeText(getApplication(), "Alarm set in " + time + " minutes",

Toast.LENGTH_SHORT).show();

/** Code for reminder is over */

我的收件人代码是

public void onReceive(Context context, Intent intent) {

String sms= "Your turn is about to come. Please be ready. Thank You";

String phoneNo;

Bundle extrasBundle = intent.getExtras();

phoneNo=extrasBundle.getString("phoneNo");

try {

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage(phoneNo, null, sms, null, null);

Toast.makeText(context, "SMS Sent to " + phoneNo,Toast.LENGTH_LONG).show();

} catch (Exception e) {

Toast.makeText(context,"SMS faild, please try again later!",Toast.LENGTH_LONG).show();

e.printStackTrace();

}

}

万一有人可能需要这个……谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值