我将开发一个应用程序,仅在2小时内每隔10分钟使用alertDialog提醒用户,
我做了一个提醒用户每10分钟的应用程序,但我想在2小时后停止它实际上我不能这样做,
如果我使用amManger.cancel(),我搜索但没有答案;它将在2小时前取消警报:“( 那么,有没有办法做到这一点? 请给我一个expamle,因为我是android的新手...
谢谢alooooot ..
--------------更新后---------------------------
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();
问题: - 当bCancel按钮自动点击时,我应该把代码放在哪里停止闹钟?
- 如何在两小时后点击???
感谢,
答案 0 :(得分:1)
试试这个 -
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));
答案 1 :(得分:0)
您可以创建另一个警报,在2小时后取消主警报。这看起来是最简单的解决方案。