我的應用程序有一個服務,每30秒執行一次特定URL的獲取。該服務使在AndroidManifest.xml配置:我想我的應用程序在後臺運行並未被Android關閉
<receiver
android:name="com.analisesistemas.taxiweb.android.alarm.MyAlarmReceiver"
android:process=":remote" >
</receiver>
<service
android:name="com.suporte.motoweb.chegaja.MyTestService"
android:exported="false" />
服務由我的應用程序啓動時這樣說:
// Setup a recurring alarm every half minute
public void scheduleAlarm() {
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, TIMER, pIntent);
}
的方法scheduleAlarm()被調用在我的主要活動的onCreate()。
問題是,當我按Home鍵(以最小化我的應用程序),所以我可以使用其他應用程序,如Facebook和Instagram ...我注意到我的應用程序停止運行(android關閉我的應用程序可能會節省內存)。
真的,我已經嘗試了各種方式,但Android在後臺運行時關閉了我的應用程序。不是馬上,它是幾分鐘後!
我該如何防止我的應用程序被Android操作系統終止?我需要讓它無限期地運行! :(
感謝自從關注
每30秒發出一次GET請求將導致數據使用量過大和電量耗盡,這將導致所有用戶卸載您的應用。詳細說明你正在嘗試解決的問題,我可以給你一個更好的解決方案,以便如何最好地解決它https://developer.android.com/training/efficient-downloads/index.html –
這種行爲可能是ROM的依賴,而不是Android(通常)的依賴。我記得在4.0.1中有一個類似的錯誤,如果我記得正確的話,但是它在很久以前就已經在更高版本中被修復了。 –