2012-05-07 133 views
0

任何人都可以告訴我如何停止此警報?停止報警Android應用程序

我試圖阻止它通過使用處理程序,但它並沒有停止它繼續重複?

這裏是我的代碼:

// ===================================後更新=================================

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

回答

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

     ..... 
+0

感謝烏拉圭回合的響應找到,但它並沒有停止過:」 – Monerah

+0

怎麼設置我最後呢?相同的原因 – Kickaha

+0

yeal所有最終變量,但它不會停止讓我顯示你我更新的代碼.. – Monerah

0

嗨,你可以使用下面的命令通過你的未決意圖:

am.cancel(yourPendingIntent); 

我已經寫上AlarmManager一個很好的教程幾個月前,你可以在這個link

+0

感謝你的迴應,但我想停止在特定時間的報警:「 – Monerah

+0

我認爲你可以安排另一個單一的報警這是因爲如果你的活動關閉了,你不能對它進行任何控制,另一種選擇可能是一種服務,但我認爲這是如此愚蠢和重量, –

+0

再次感謝,我試圖做另一個警報,但我不知道,請你多解釋一下! – Monerah