问题
I've to develop an application that remindes the user every 10 minutes with alertDialog during 2 hours ONLY,
I made an application that reminds a user for each 10 minutes, but I want to stop it after 2 hour actually I couldn't do that,
I searched but no answers, if I used amManger.cancel(); it'll cancel the alarm before 2 hour :"( so, is there any way to do that ? and please give an expamle as I'm new to android...
thanks alooooot ..
-------------- After Updating ---------------------------
public class MyAlarm extends Activity {
private PendingIntent pendingIntent;
int i = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonStart = (Button) findViewById(R.id.startalarm);
Button buttonCancel = (Button) findViewById(R.id.cancelalarm);
Button bCancel= (Button) findViewById(R.id.button1);
pendingIntent = PendingIntent.getService(MyAlarm.this, 0,
new Intent(MyAlarm.this, MyAlarmService.class), 0);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
long firstTime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager am2 = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime + 10 * 1000, 10 * 1000, pendingIntent);
}
});
bCancel.performClick();
Question: - Where should I put the code to stop the alarm when the bCancel button clicked automatically ??
-and how can I make this click after two hours ???
thanks,
回答1:
Try this-
final Button bCancel= (Button) findViewById(R.id.button1);
bCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Your code to stop the alarm goes here.
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
bCancel.performClick();
}
}, (2*60*60*1000));
回答2:
You can create another alarm that will cancel your main alarm after 2 hours. This looks like the simplest solution.
来源:https://stackoverflow.com/questions/9716573/how-to-stop-alarm-android-application