android


startService restart my service if currently running, the Android docs say the opposite


in my service, I download a lot of stuff from the internet and would like to do this every x hours (choice in Preferences)
The current implementation I have written in this code in the main Activity:
Intent svc = new Intent(this, AlarmService.class);
if (startService(svc)!=null) {
Log.i(this.getClass().toString(), "Service already running");
} else {
Log.i(this.getClass().toString(), "Service Started");
}
Because, according to the docs
Request that a given application service be started. The Intent can
either contain the complete class name of a specific service
implementation to start, or an abstract definition through the action
and other fields of the kind of service to start. If this service is
not already running, it will be instantiated and started (creating a
process for it if needed); if it is running then it remains running.
That last sentence that I highlited let me hope that I could write all my heavy code in the onCreate of the service and it will be untouched, but unfortunately, everytime the user starts the app, the service starts again and make a huge download, draining the battery.
The indication if the service is running or not is correct:
Returns If the service is being started or is already running, the
ComponentName of the actual service that was started is returned; else
if the service does not exist null is returned.
So, my question, How can I let the service untouched if already started, and start it if not yet started?
Thanks
EDIT:
The service in never stopped:
public class AlarmService extends Service {
Alarm alarm = new Alarm();
public void onCreate() {
super.onCreate();
Log.i("**", "onCreate");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// DO NOTHING FOR TESTING alarm.SetAlarm(this);
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
in my service, I download a lot of stuff from the internet and would like to do this every x hours (choice in Preferences)
Hopefully you are doing this with an IntentService (possibly my WakefulIntentService) and AlarmManager.
That last sentence that I highlited let me hope that I could write all my heavy code in the onCreate of the service and it will be untouched, but unfortunately, everytime the user starts the app, the service starts again and make a huge download, draining the battery.
That is because either the service had been stopped (via stopService() or stopSelf()), or because your app's process was terminated.
How can I let the service untouched if already started, and start it if not yet started?
Call startService(), as the documentation indicates.
But if you are really trying "to do this every x hours", the user will really appreciate it if your service does not waste RAM trying to stick around during that time. Hence, the user or the OS may well get rid of your process during the "x hours". If you do not want to do the "huge download" every "x hours", then you need to save that information locally to some sort of file.

Related Links

Inter fragment communication after fragment creation
Update EditText in a list view with value from a SeekBar
Bluetooth BLE Android write to peripheral with max throughput
Finding a point after android canvas rotation
Change ImageView background in a Row of ListView
How to Implement our own (customize) server code for Openfire
How to apply the grayscale effect to the Bitmap image?
Android Video Capturing by intent getting Error?
Google Place API used application working only android 2.3 not in the above versions
How to create a layout like in picture in android?
How to change the settings logo in Action Bar in Android?
Eclipse with ADT won't start - points at error in /metadata/.log file
Scrollup not working in StickyListviewHeader with SwipeRefreshLayout
Android: Move a view along with finger
Android is not picking right dimens.xml file from values folder
Custom tabs design with Swipeable Views

Categories

HOME
httparty
fabricjs
porting
ggplot2
telegram
nvd3.js
mjml
serialization
shiro
google-cloud-functions
simulink
pearson-correlation
tvos
python-multithreading
frontend
odoo-8
sendgrid-api-v3
fosrestbundle
automatic-ref-counting
entitymanager
template-engine
html5-appcache
git-extensions
swagger-2.0
charles
windows-xp
r-mice
inno-download-plugin
qliksense
dapper-extensions
windows-server-2003
asymptotic-complexity
has-and-belongs-to-many
handler
angular-translate
quantlib
alm
memory-address
fossil
jwplayer6
wurfl
ideascript
nohup
password-protection
bc
odroid
metawidget
logback-groovy
oracle-service-bus
dapper-simplecrud
httr
phpdbg
jide
clarifai
yii2-api
user-defined-fields
datamaps
posixct
payara-micro
stdmap
subnet
testrail
pure-css
flex-monkey
dimension
swizzling
jtwig
bizagi
aspen
linklabel
gocql
sapscript
finalbuilder
sql-server-data-tools
telepat
nsurlsessiondatatask
xmltype
ejb-2.x
nodelist
jbake
sonarqube-5.0
post-commit-hook
device-tree
raygun
encryption-asymmetric
named-parameters
mintty
paste
maven-scm
websphere-esb
scrypt
c++builder-xe5
gpars
qi4j
pstack
boost-msm
iboutlet
jdb
failing-tests
catransform3d
clrstoredprocedure
timertask
binomial-heap
data-mapping
web-application-project
sun
asynchronous-wcf-call
alsb

Resources

Encrypt Message