0

can anyone help me to solve my problem, I've to make the alarm goes off after 10 minutes from current time and it must be repeated for each 10 minutes I wrote this code but it didn't work, it start in random :"

public class AlarmNewActivity extends Activity {
 /** Called when the activity is first created. */
 Intent intent;
 PendingIntent sender;
 AlarmManager am;
 Button bStart, bStop ;
 long mCurrentTime, firstTime ;
 Calendar calendar;
 TextView tv;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 bStart = (Button) findViewById (R.id.button1);
 bStop = (Button) findViewById (R.id.button2);
 tv = (TextView) findViewById (R.id.textView1);
 intent = new Intent(AlarmNewActivity.this, RepeatingAlarm.class);
 sender = PendingIntent.getBroadcast(AlarmNewActivity.this, 0, intent, 0);
 am = (AlarmManager)getSystemService(ALARM_SERVICE);
 calendar = Calendar.getInstance();
 calendar.setTimeInMillis(System.currentTimeMillis());
 mCurrentTime = calendar.getTimeInMillis();
 bStart.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 tv.setText(""+ firstTime + "\n" + mCurrentTime );
 am.setRepeating(AlarmManager.RTC_WAKEUP,
 mCurrentTime + 10 *1000, 5*1000, sender);
 new Handler().postDelayed(new Runnable() {
 public void run() {
 bStop.performClick();
 }
 }, ( mCurrentTime + 50 * 1000 )); 
 }
 });
 //==================================================================================
 bStop.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 am.cancel(sender);
 }
 });
 }
}
asked May 7, 2012 at 15:24
0

1 Answer 1

3

mCurrentTime = calendar.getTimeInMillis();

Should be inside the onClick to get the current time when clicked

public void onClick(View v) {
mCurrentTime = calendar.getTimeInMillis();
...
...
}
answered May 7, 2012 at 15:30
Sign up to request clarification or add additional context in comments.

2 Comments

thanks alot that works and I must put calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); inside the button too .. so appreciated thanks..

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.