停止警报Android应用程序 [英] Stopping Alarm Android Application

查看:65
本文介绍了停止警报Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何停止此警报吗?

Can anyone tell me how can I stop this alarm?

我正在尝试使用处理程序来停止它,但并没有停止,它会继续重复

I'm trying to stop it by using a handler but it didn't stop it continues to repeat?

这是我的代码:

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

//=================================== After Updating =================================

     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);
            }
        });
     }
}


推荐答案

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

make alarm manager global so that the various functions are referencing the same object

 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);

        .....

这篇关于停止警报Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆