Start a service from a service class in android - 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);).

Related

Oreo Background Service Issue

Please I need a quick look into this.
For classes that extends Service directly not IntentService, How do you start such services??
I have just been seeing extension of IntentService which doesn't suit my case study. Thanks
Check the first paragraph of the official documentation:
Services can be started with Context.startService() and Context.bindService().
Also check out services overview, it has nice explanations and examples, e.g.:
Create a service by extending Service class:
public class HelloService extends Service {
...
}
Start a service:
Intent intent = new Intent(this, HelloService.class);
startService(intent);

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.

Broadcast receiver works only when device reboot android

Hi I am developing android application in which I am defining one broadcast receiver.I am calling receiver from my activity. I am defining broadcast receiver like this :
public class MyScheduleReceiver extends BroadcastReceiver {
private static final long REPEAT_TIME = 100 * 5;
#Override
public void onReceive(Context context, Intent intent) {
Log.i("RRRRRRRRRRRRRRRRRRRRRRRR", "on receive");
}
}
In android manifest file I am defining like this:
<receiver android:name="abc.xyz.MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and in main activity I am calling my broadcast receiver like this :
//in activity oncreate
startService(new Intent(this, MyScheduleReceiver.class));
My problem is that when call start service it's not starting my service actually. But when i restart my device it start my service because I gave intent filter "BOOT_COMPLETED". what I wanted to do actually when i call start service my service must be start,
Am I doing something wrong. How to solve this problem?
Actual what happens here is that you can staring a broadcast receiver while starting the activity and this broadcast receiver starts listening BOOT_COMPLEATED is happening or not. When this happens it comes to onreceive . If you need to start a process doing in background you can use a a Service insted of BroadcastReciever. BroadcastRecievers are used to listen for some events to happen.Go through this, it will help you
Services
BroadcastReceiver
You're either confused, or you aren't wording your question well. What you have in your manifest (and how Android works generally) is that when BOOT_COMPLETED occurs, it will call that BroadcastReceiver you defined. It will not automatically start an activity or service. If you want to do that, you need to call startService or startActivity in your onReceive function of the receiver.
You do not start BroadcastReceivers. You start services, which are long term background processes. You register BroadcastReceivers to be informed of special events (like BOOT_COMPLETED). When one of the events you registered for occurs, it will create an instance of that class and call its onReceive.
Hopefully that clears things up. If not, I suggest you reread some tutorials on services and broadcast receivers, you seem to have the two confused.
startService call would only start a Service. MyScheduleReceiver here is a braodcast receiver. To trigger broadcast receivers, you generally have to send broadcasts and not call the startService.
to start broadcasts you need to send broadcasts not startService()
add this instead of startService(new Intent(this, MyScheduleReceiver.class));
Intent intent = new Intent();
intent.setAction("pakagename.MyScheduleReceiver");
sendBroadcast(intent);
I hope it helps.

Trying to start a private service

I'm trying to start a service. This service is not meant to be called by other application.
I am trying to call startService with an Intent that was created using Context and Class parameters. I'm trying to use createPackageContext(getPackageName(), CONTEXT_IGNORE_SECURITY) to get the package context. startService however is having no effect, onStartCommand is never being called.
How do I start a service that is within the same package?
How do I start a service that is within the same package?
startService(new Intent(this, MyServiceClass.class));

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

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.

Categories

Resources