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
Monerah
2151 gold badge5 silver badges17 bronze badges
1 Answer 1
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
Kickaha
3,8678 gold badges41 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Monerah
thanks alot that works and I must put calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); inside the button too .. so appreciated thanks..
Monerah
can you see this please: stackoverflow.com/questions/10485358/…
lang-java