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));
Related
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.
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);).
My question is as follows:
I started my intentService from my main Activity. This intentService does some audio processing with audioRecord.
However, when I need to start another activity in my application (recording video in this case), i need to stop the intentService in the background (because it is hogging the audio resource).
Is there a way to stop the intentService from the main activity?
You can use stopService() from your main activity like this:
stopService(new Intent(yourMainActivity.this,yourIntentService.class));
use the same pre-declared intent which you used to start the IntentService and call
stopService(intent);
if you create a new intent and use it to stop the service, the service will not respond to it until its finishes processing the previous intent.
but it should be noted that this will not immediately stop the service. So a workaround to this is to have a Global boolean variable. When its set to true the processing within the service will carry out, and when you want to end it set the boolean variable to false from your activity. and then you can stop the service from within itself by calling
stopSelf();
How can I get the service reference in a child activity.
Basically I started a service called "A" in activity1, I pass the intent to activity2 and started the activity2. How can I get the reference of my service "A" in activity2.
code in Activity1:
getApplicationContext().bindService(new Intent (this, AndroidUpnpServiceImpl.class), serviceConnection, Context.BIND_AUTO_CREATE);
It will start the service and bind the service to serviceConnection
Now I want the reference of same service in activity2
Thanks.
Looks like you can simply call bindService in your activity2; services are singletons so another service object will not be spawned but you'll connect to the existing one.
Check the discussion regarding this topic.
Current I have three classes: activity (A), broadcastReceiver (B) and service (C).
Assuming that A is binding to C and now, B get a new intent from system.
Can B bind to the C (exactly the same one) directly?
I find out that there is a peekService method in broadcastReceiver.
my question is can I bind the running service in broadcastReceiver?
If you have registered your receiver dynamically with Context.registerReceiver() then you can bind to a Service from the onReceive method.
However, If you have declared your BroadcastReceiver in the manifest, then you should not bind to a Service from the onReceive() method. You can start a Service though - you just cannot bind to it because bindService() is asynchronous. More details about this in the Android Dev Guide and the onReceive documentation.
I find out that using peekService can bind the running service.
Such like
IBinder ib = peekService(context, new Intent(context,
xxxxService.class));
((xxxxService.class)ib). do anything here...