java - 单击按钮时启动 AlarmManager

标签 java android alarm android-alarms

我想在按下按钮时启动 AlarManager。问题是:当我启动应用程序时,AM 就会启动:/

我的代码:

public void scheduleAlarm()
    {  
            int time = 10 * 1000;

            intentAlarm = new Intent(this, AlarmReciever.class);

            alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, time, time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

    }

我尝试在按钮的 onClickListener 中调用它。但它从 Activity 开始开始:/

有人可以帮助我吗?

最佳答案

感谢您发布您的代码。试试这个:

public static String ALARM_TO_SET = "ALRMTOSEND";
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
     int time = 10 * 1000;

    intentAlarm = new Intent(ALARM_TO_SET);

    alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intentAlarm, 0)
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, time, time, pIntent);


  }
});

您的广播接收器应在 Manifest.xml 文件中注册为:

<receiver android:name=".AlarmRecieverClass">
<intent-filter>
<action android:name="ALRMTOSEND" />
</intent-filter>
</receiver>

关于java - 单击按钮时启动 AlarmManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742257/

相关文章:

android - 带有 BottomSheetBehaviour 的 MotionLayout 中的 RecyclerView 不会在触摸 RecyclerView 时滚动

java - 如何从匿名类更新java中的全局变量

c - 调用函数 popen 中断进程

java - 如何使用模仿日常程序的模拟对象?

Android AlarmManager 在长时间后停止触发

java - 在哪里可以关闭我的 Spring Boot 应用程序中的 Lettuce Redis 连接?

java - 在 JUnit 测试的 @Before 方法中使用断言语句?

android - 如何让软键盘覆盖一个 View ,同时让其他 View 占用可用空间?

java - 使用一种布局并在 Eclipse 中以编程方式更改字符串

java - 非英语短信显示为多个字符串?