Reputation: 215
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);
}
});
}
}
Upvotes: 0
Views: 176
Reputation: 3857
mCurrentTime = calendar.getTimeInMillis();
Should be inside the onClick to get the current time when clicked
public void onClick(View v) {
mCurrentTime = calendar.getTimeInMillis();
...
...
}
Upvotes: 3