Pop_up_2.class
Intent i = new Intent(Pop_up_2.this, Smscreator.class);
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
c.set(Calendar.HOUR_OF_DAY,mHour);
c.set(Calendar.MINUTE,mMinute);
c.set(Calendar.SECOND,00);
c.set(Calendar.YEAR,y);
c.set(Calendar.MONTH,m);
c.set(Calendar.DAY_OF_MONTH,d);
aManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pIntent);
Toast.makeText(getApplicationContext(), "Sms scheduled: " + message, Toast.LENGTH_SHORT).show();
sendBroadcast(i);
Smscreator.class
public class Smscreator extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(no, null, message, null, null);
Toast.makeText(context, "SMS sent.",
Toast.LENGTH_LONG).show();
}
}
我一直试图在用户指定的日期和时间发送SMS,但是SMS立即被发送。我已经很长时间试图找出错误了,但是我找不到任何错误。我的活动和广播接收器均已在AndroidManifest中声明。有人请给我关于如何实现此目标的正确答案。
答案 0 :(得分:0)
我实现了给定时间的通知时间表。 只需将此代码作为参考:
public void scheduleNotification(Context context, long delay, int notificationId) {//delay is after how much time(in millis) from current time you want to schedule the sms
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.title))
.setContentText(context.getString(R.string.content))
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.app_icon)).getBitmap())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent intent = new Intent(context, YourActivity.class);
PendingIntent activity = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(activity);
Notification notification = builder.build();
Intent notificationIntent = new Intent(context, MyNotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notificationId);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
public class MyNotificationPublisher extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(no, null, message, null, null);
Toast.makeText(context, "SMS sent.",
Toast.LENGTH_LONG).show();
}