0

我想在按下按钮时启动 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 中调用它。但它从活动开始:/

谁能帮我?

4

4 回答 4

1

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

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>
于 2013-07-19T12:24:32.817 回答
0

你为什么不为此使用 timerTask ..

于 2013-07-19T09:13:35.563 回答
0

您应该在方法setRepeating内移动调用onClickListener

于 2013-07-19T09:12:23.430 回答
0

尝试这个:

Button yourButton = (Button)findViewById(R.id.yourId);
yourButton.setOnClickListener(new OnClickListener(){
   public void onClick(View view){
         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));


   }
  });
于 2013-07-19T10:08:36.433 回答