警报管理器立即触发

编程入门 行业动态 更新时间:2024-10-24 07:23:35
本文介绍了警报管理器立即触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用AlarmManager。我写了下面给出的代码。根据代码,AlarmManager应该在10秒后触发,但是在我的代码中,警报管理器会立即触发。请帮忙。

Hi I am currently working with AlarmManager. I have written a code given below. As per code the AlarmManager should be triggered after 10 Sec, but here in my code the alarm manager triggers immediately. Please help.

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; long timeOrLengthofWait = 10000; Intent intentToFire = new Intent(this, AlarmReciever.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0); alarmManager.set(alarmType, timeOrLengthofWait, alarmIntent); } }

我的AlarmReciever类别

And My AlarmReciever Class

public class AlarmReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String phoneNumberReciever="5556"; String message="Alarm Triggered"; SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumberReciever, null, message, null, null); Toast.makeText(context," A message has been sent", Toast.LENGTH_LONG).show(); Log.d("Alarm ", "Alarm Has been triggered and sms send"); } }

我已经在清单中添加了必需的权限。

I have Already added required permissions in manifest.

推荐答案

您使用的警报类型为 ELAPSED_REALTIME_WAKEUP 。这意味着 set()的第二个参数必须是从现在起的毫秒数,现在表示为 SystemClock.elapsedRealtime()。

You are using an alarm type of ELAPSED_REALTIME_WAKEUP. That means that the second parameter to set() must be the number of milliseconds from now, where now is expressed as SystemClock.elapsedRealtime().

如果您的目标是从创建 set()调用,则 set()调用应为:

If your goal is to have this occur 10000 milliseconds from the time you make the set() call, that set() call should be:

alarmManager.set(alarmType, SystemClock.elapsedRealtime()+timeOrLengthofWait, alarmIntent);

更多推荐

警报管理器立即触发

本文发布于:2023-11-27 03:35:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636407.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!