Can I get the instance of the service class that I created - android

If I create a Service like this:
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
Can you please tell me how can my code else where get the instance of that service class that I start?
And if i start Service, does that class runs in its own thread?
Thank you.

You have to bind to the service.
And the service is started asynchronously, but any code that's run in the service is run on the main thread. In fact, everything in android is run on the main thread unless you specifically create another thread.

Related

android.os.NetworkOnMainThreadException exception while trying to call a webservice from my android Services?

I have a scenario where I need to call the Webservices from the Android Services.
Currently I am starting the Services from the Activity by
Intent mServiceIntent = new Intent(this, AServices.class);
if (!isMyServiceRunning(AServices.class)) {
startService(mServiceIntent);
}
But I am getting an exception as android.os.NetworkOnMainThreadException. As I am following MVVM Architecture so is there any way or alternative that I can call it from AsyncTask and any other stuffs?
Service runs on the UI Thread. If you need a Service extend IntentService or the newly JobIntentService and not Service. Both IntentService (onHandleIntent) and JobIntentService (onHandleWork) will be executed on thread != from the UI/Main thread

After moving BluetoothActivity into service class.How to send data to service from another activity to control led

I'm using service to run Bluetooth in background,but i don't no how to send data from another activity to service
Service and other activities work in the same process
To start a Service, use following statement:
Intent intent = new Intent(context, Service.class);
// intent put some extra
startService(intent);
Calling this statement firstly, Service call onCreate() method and onStartCommand().
Not firstly, Service just call onStartCommand().
You can check if it is called firstly by extra in intent.
So you can send data with the same method startService(Intent intent).
Service works in another process
use [bindService](https://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)) and AIDL
It is suggested that make service and other activities work in the same process when your target is not complex.

Start a service from a service class in android

Is it possible to start a service class from another service class?
Intent localIntent = new Intent("com.example.android");
startService(localIntent);
I have tried the above code but I do not think it works.
Is it possible to start a service class from another service class?
AFAIK, you can start a service from any Context.
I have tried the above code but I do not think it works.
Well, hopefully your service does not have an <intent-filter> for an action string of com.example.android. Use an explicit Intent to identify your service (e.g., new Intent(this, YourServiceClass.class);).

Start intent service

I start an Intent Service this way:
Intent MyIntentService = new Intent(this, clsMyIntentService.class);
MyIntentService.putExtra("Command", Command1);
startService(MyIntentService );
....
Sleep and do some work
....
Intent MyIntentService = new Intent(this, clsMyIntentService.class);
MyIntentService.putExtra("Command", Command2);
startService(MyIntentService );
My problem is that the IntentService does not receive anything until everything is done.
And when it starts receiving the order is wrong because Command2 is received before than Command1 (right before).
Any help with this?
Implement your service as an IntentService Type, it has it's own worker thread to avoid blocking the UI and it queues the requests to start it so your commands will be executed in the correct order.
check these links for more info:
http://developer.android.com/reference/android/app/IntentService.html https://developer.android.com/training/run-background-service/create-service.html

Android start service issue in onCreate of Activity

I have tried to start a service and bind to the service in my Activity's onCreate() method. When I try to call a function from service like commSessionManagerService.startCommandUpperM() afterwards, a NullPointerException occurs. Here is the code that I use to start the service and bind to it:
Intent startIntent = new Intent(this, CommSessionManagerService.class);
startService(startIntent);
Intent bindIntent = new Intent(this, CommSessionManagerService.class);
bindService(bindIntent, conn, Context.BIND_AUTO_CREATE);
If I move the function startCommandUpperM() to onStartCommand() in the CommSessionManagerService, the onCreate method will take several seconds to complete. As a related note, I have a created and started a thread in the startCommandUpperM() function.
This is because your Service is actually bound on the UiThread. As onCreate also runs on UiThread, your call to bindService result in Handler.post(Runnable) be called on the main thread's handler.
So when bindService returns, the Service isn't already bound.
To circumvent this problem, you should put your code using your Service inside ServiceConnection.onServiceConnected().

Categories

Resources