How to Start android service without activity - android

I created an application that contain a Service that start from button click of Activity.
But I want to create a new application that doing the same as the last one - but don't have any GUI interface => that mean without any Activity.
I want to make the application to start on device boot and never stop (until the device is shutdown) .
How to do it ?
Is there any way ?

How to do it ?
By using Context.startService() or Context.bindService() methods.
where Context might be Activity, Application, argument of the onReceive() method of BroadcastReceiver etc.

Related

Run start service when application starts and run across multiple activities

I want to write an android service that will run with an app. I want to run this service across multiple activities and have the ability to pass information back to the UI with all activities, not just the activity that starts the service.
From what I understand, I have call startService and not bind it to the activity. Is this correct?
Also, I do not want to start a new instance of the service, I want to use the same instance. If i call startService in each activity, will that use the same, running instance of the service?
startService will start the service only if there's not a previous service running, it won't spawn a second service.
Once you start your service, you can connect to it from any Activity you want, but to pass information back and forth, you need to bind to the service, so you can obtain a pointer to it.
Make sure to read Android's documentation, because the way services work is a little confusing at the beginning.

Will auto start service also launch the Application?

In my App, I have a remote service will auto start once receive "ACTION_BOOT_COMPLETED" broadcast. In this App I also have a class which extend "Application" class as the App entry point.
My question is when the service is launched by the Andriod OS when the device finish reboot, will the method "onCreate()" in the Application class get called first??
What if the service is local, will it make any difference?
I would think that the onCreate() method WILL be called for the application to start. Since it is the natural flow of the activity life cycle.

How to detect user activity/inactivity in Android service/background application?

How to detect user activity/inactivity in android service/background application - from same or other applications?
I want to know that user stop touching screen and not busy with another activity to show notifiation popup. Not sure is there no other method than use AccessibilityService.
One of the ways is to know that from reading the Android logs
Use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the system is not booted) and have the AlarmManager send an Intent for another receiver.
I think they answered it in this question to do what you are looking for:
android: running a background task using AlarmManager
Define a interface in activity and service.
in oncreate of activity set reference to activity in service method.
Now you have activity reference for entire service lifetime.
another way to do this is, application class, same application class is shared by activity and service, so you can both of these from each other using application class.
If you want see from applications then use broadcastreceivers that is the only wau

A Local Service implementation closely related to Application Life Cycle

In my App. I have extended Application class, say MyApp and I have few activities say A,B,C,D where A is the default Activity or root Activity. I want to start a Service (say MyService) inside OnCreate of the MyApp and stop it when User logs out from my Application,
However I need a strong way to communicate between my Activities (A,B,C,D), MyApp and MyService. Now Application is launched second time from the Memory. OnCreate of MyApp will not be called so I would like to start the Service from OnClick of the SignIn button which is inside activity A. Now I have a number of questions :-
I have tried both startService and bindService. Problem with startService is I don't get back the Service reference for future use. If I use bindService will that make my Service visible through out the Application life cycle till I explicitly stop it. Which way is applicable in my case ?
What is the best way to implement communication between Service and MyApp / Activities and vice a verse ? By Communication I mean two type of communication, 1. that needs UI thread i.e. start some activity, show some dialog etc. 2. Something like myService.doSomehing().
The Service has a thread which does network communication which should not be closed during the life time of the Application. START_STICKY will work for me or I need to make extra arrangements for that.
This app is supposed to run for GingerBread+ devices...
Bind the service inside your extended app oncreate and create a public method
(getBinding) which returns that binding object ... In your root/default activity 'A' oncreate via getBinding method on casted getApplication object retrieve .. unbind when all activities close (keep counter with each getBinding method)
check this Cleanly binding/unbinding to a Service in an Application
so for 1) Bind service
2) the binding obj
3) START STICKY would be enough

Start activity/send intent from object instance within service

I have the following situation: A service is running in the background of my application and regularly receives UDP packets. It uses an instance of my HandleMessageAgent class which analyses every message and shall start a new activity if necessary.
I would like to perform the following task: No matter which activity is in the front (as long as the service is running) I would like to inform the user about an incoming message under certain circumstances. I also need to update the information regularly as long as it is valid. Afterwards it should be closed automatically.
At first I thought about using a Dialog, but I think I cannot use it when the activity is not visible. Therefore I decided to use an activity, as it can be started from a service all the time.
I want to start the activity within the HandleMessageAgent object (in a method). My problem is, that I do not know how I can define an Intent to start an activity within an object, as the Context is not clear to me.
Is there a more elegant way to perform this task? Or can anybody help me with starting an activity from an object method within a service? Thank you!
There are two situations to consider:
When your service need to notify user your activities are not active, because some other app is active. In this case you should notify user via a system-preferred way: Android Notifications. You should not forcefully show dialogs or activities if user is using some other app. That's what notifications are for.
If one of your activities is active (no matter which) then your service should send a broadcast and interested activities should listen for it and act upon it. That way your service will not depend upon specific activity and will not need to keep track which activities are active at the moment the notification must be shown.
You can make your object Parcelable and add it to the Intent that will start the Activity.
Or you can put it in a subclass of Application because that instance is shared between your activities and your services (as long as they're in the same process)
You might need the "START_NEW_TASK" flag on the Intent

Categories

Resources