2012-12-15 40 views
1

我試過四處搜尋,但似乎我是唯一一個遇到這種錯誤的人,所以也許有人會指出我做錯了什麼。鬧鐘管理器OnCreate()隨機調用

我通過此功能,創建一個AlarmManager

public void startTimer() { 
     Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class); 

     pendingIntent = PendingIntent.getService(MainActivity.this, 0, 
       myIntent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.add(Calendar.SECOND, 10); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_FIFTEEN_MINUTES/50, pendingIntent); 

     Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG) 
       .show(); 
    } 

預期它創建:OnCreate()被稱爲然後OnStart()被調用。 後來我通過調用函數停止計時器:

private void stopTimer() { 
     Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class); 

     pendingIntent = PendingIntent.getService(MainActivity.this, 0, 
       myIntent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.cancel(pendingIntent); 
    } 

正是這個電話後確實停止了。

之後發生的問題,當AlarmManager的OnCreate()OnStart()方法被稱爲長的應用程序與這些調用不容易看到系統關閉後。雖然這似乎發生在我的手機沒有可用內存的時候。

任何想法?提前致謝!

回答

0

完成後,您的服務需要撥打stopSelf(),或者您需要切換到使用IntentService,該服務完成後將自動撥打stopSelf()。目前,你正在泄漏該服務,Android會在一段時間後銷燬並重新創建。

+0

哦,謝謝,我錯過了它。這似乎解決了我的問題。 – niosus