特定日期和时间的SMS计划

问题描述 投票:0回答:1

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


    }
}

我一直在尝试在用户指定的日期和时间发送短信,但短信会立即发送。我一直试图找出错误很长一段时间,但我找不到任何错误。我的活动和广播接收器都已在AndroidManifest中声明。有人请给我一个正确的答案,我怎么能实现这个目标。

java android alarmmanager android-broadcastreceiver
1个回答
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();
}
© www.soinside.com 2019 - 2024. All rights reserved.