0

谁能告诉我如何停止这个警报?

我试图通过使用处理程序来阻止它,但它没有阻止它继续重复?

这是我的代码:

//====================================更新后=========== =======================

     Button bStart, bStop ;
     long mCurrentTime;
     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);

        final Intent intent = new Intent(AlarmNewActivity.this, RepeatingAlarm.class);
        final  PendingIntent sender = PendingIntent.getBroadcast(AlarmNewActivity.this, 0, intent, 0);
        final AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);



        bStart.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                mCurrentTime = calendar.getTimeInMillis();
                //tv.setText(""+ mCurrentTime );

                 new Handler().postDelayed(new Runnable() {
                        public void run() {
                             bStop.performClick();
                             tv.setText(""+ mCurrentTime );
                        }
                    }, ( mCurrentTime + 30 * 1000 ));
                am.setRepeating(AlarmManager.RTC_WAKEUP,
                        mCurrentTime + 10 *1000, 5*1000, sender);


            }
        });



        //==================================================================================

        bStop.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                am.cancel(sender);
            }
        });
     }
}
4

2 回答 2

0

使警报管理器全局化,以便各种功能引用同一个对象

 Button bStart, bStop ;
     long mCurrentTime;
     Calendar calendar;
     TextView tv;
     AlarmManager am
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       ...
       ...

        Intent intent = new Intent(AlarmNewActivity.this, RepeatingAlarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(AlarmNewActivity.this, 0, intent, 0);
        am = (AlarmManager)getSystemService(ALARM_SERVICE);

        .....
于 2012-05-07T16:34:21.827 回答
0

您好,您可以使用以下命令传递您的待处理意图:

am.cancel(yourPendingIntent);

几个月前我在 AlarmManager 上写了一个很好的教程,你可以在这个链接上找到

于 2012-05-07T17:19:10.967 回答