SMS schedule at a particular date and time

42 views Asked by At

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();


    }
}

I have been trying to send the SMS at the user specified date and time, but the SMS gets sent immediately. I have been trying to find out the error for a long time, but I can't find any. Both my activity and my broadcast receiver have been declared in AndroidManifest. Someone please give me a proper answer on how I can achieve this.

1

There are 1 answers

1
Prafulla Malviya On

I implemented for notification schedule on given time. Just take this code as reference:

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();
}