2011-09-14 159 views
0

我正在編寫一個應該在後臺每10分鐘運行的程序。只要我積極使用我的手機,我的代碼似乎工作得很好,但經過很長一段時間後,說一夜之間,這個過程似乎停止了。當我的程序正在運行時,應該可以在設備上的「緩存進程」下查看它,但之後它會在列表中停止顯示。AlarmManager停止運行

我在讀WakefulIntentService,想知道是否需要使用它。據我瞭解,即使手機在睡覺,它也會讓您的後臺進程繼續運行。不確定Android中的「睡眠」意味着什麼,如果是關機的時候,或者手機進入「睡眠」狀態,如果它暫時不用的話。

這是我使用的代碼:

主要類:

public class Main extends ListActivity 
{ 
     @Override 
     public void onCreate(Bundle icicle) 
     { 
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

      Intent myIntent = new Intent(this, AlarmReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0); 
      alarmManager.cancel(pendingIntent); 

      alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 600000, 600000, pendingIntent); 
     } 
} 

廣播接收器類:

public class AlarmReceiver extends BroadcastReceiver 
{ 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      context.startService(new Intent(context, MainService.class)); 
     } 
} 

服務類:

public class MainService extends Service 
{ 

protected void handleIntent(Intent intent) 
{ 
    // obtain the wake lock 
    PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); 
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); 
    mWakeLock.acquire(); // check the global background data setting 
    ConnectivityManager cm = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE); 

    if (!cm.getBackgroundDataSetting()) 
    { 
     stopSelf(); 
     return; 
    } 

    new FetchItems().execute(); 
} 
} 

private class FetchItems extends AsyncTask<Void, Void, Void> 
{ 
    protected Void doInBackground(Void... unused) 
    {   
     SomeLongProcess(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) 
    { 
     stopSelf(); 
    } 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    handleIntent(intent); 
    return START_STICKY; 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    handleIntent(intent); 
} 

@Override 
public void onDestroy() 
{ 
     super.onDestroy(); 

     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

     Intent myIntent = new Intent(this, AlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); 
     alarmManager.cancel(pendingIntent); 

     mWakeLock.release(); 
} 

的AndroidManifest.xml :

<receiver android:name=".AlarmReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

回答

2

即使設備處於睡眠狀態,是否真的必須每十分鐘運行一次?如果沒有,請使用AlarmManager.ELAPSED_REALTIME,當設備被喚醒時,您的服務將運行,節省很多電池。至於你的問題,你可以假設屏幕將會進入昏暗狀態==(當然,如果其他服務持有喚醒鎖定,情況並非如此)。

+0

這是一個很好的問題。我正在編寫的程序基本上是一個RSS閱讀器,在設置中,用戶可以指定是否需要自動更新,所以我猜測它應該在設備睡着時運行,否則它只會更新它們的提要當他們使用他們的電話。 –

+0

除非他們在喚醒手機時打開的第一件事是您的應用程序,否則您將有足夠的時間來緩存Feed。考慮到這一點:用戶(和電話)已睡了8個小時,並且在那段時間你將會運行你的服務48次。 RSS不是關鍵數據,你可能會放鬆一下你的需求,以節省一些電池。 –

+0

的確如此。我實際上已經設置了用戶可以選擇他們想要的時間間隔,並在消息中指出時間間隔越短,電池的使用就越多。他們可以選擇5分鐘到5小時的任何地方。我只是不想將它放到上面的代碼中,因爲它看起來並不相關。但問題仍然存在,爲什麼當手機暫時沒有使用時,該服務停止運行。 –