简体   繁体   English

当设置的警报响起时,如何调用OnReceive()函数?

[英]How do I invoke the OnReceive() function when a set alarm goes off?

I have been trying all day to invoke the BroadcastReceiver.OnReceive() function on the Android. 我整天都在尝试在Android上调用BroadcastReceiver.OnReceive()函数。 I am using Android AIDE for my projects, but nothing seems to work. 我的项目使用的是Android AIDE,但似乎没有任何效果。 I am using... 我在用...

        Intent openNewAlarm = new Intent(AlarmClock.ACTION_SET_ALARM);
        startActivity(openNewAlarm);

to set my alarm, and a dialog comes up that allows me to set as many alarms as I would like, but instead of a ringtone, I would like to program (through an OnReceive() function) what I would like to happen instead. 设置我的警报,然后出现一个对话框,允许我设置任意数量的警报,但是我想(通过OnReceive()函数)编程我想发生的事情,而不是铃声。 What can I do to achieve this? 我该怎么做? Thank you. 谢谢。

Try this 尝试这个

   Intent startIntent = new Intent("BROADCAST");
    PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, startPIntent);

And then in your Manifest.xml file: 然后在您的Manifest.xml文件中:

<receiver android:name="com.package.YourOnReceiver">
   <intent-filter>
       <action android:name="BROADCAST" />
   </intent-filter>
</receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM