MAkS
MAkS

Reputation: 751

Start a Service from an Activity

In my app, I have an Activity from which I want to start a Service. Can anybody help me?

Upvotes: 32

Views: 68486

Answers (7)

Ajith K P
Ajith K P

Reputation: 396

If you want to start a service and it should run in background use START_STICKY in your corresponding service.

You can start service with on boot also,

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

And create receiver,

<receiver android:name=".auth.NotificationBroadcast" android:enabled="true" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

In Brodcast Receiver add,

@Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver");

        if (intent != null) {
            String action = intent.getAction();

        switch (action) {
            case Intent.ACTION_BOOT_COMPLETED:
                System.out.println("Called on REBOOT");
                // start a new service 
               startService(new Intent(getApplicationContext(),Servicename.class));

                break;
            default:
                break;
        }
    }
}

And your service is like,

Upvotes: 0

Sanam Yavarpor
Sanam Yavarpor

Reputation: 354

in kotlin you can start service from activity as follow :

   startService!!.setOnClickListener { startService() }
 
   private fun startService(){
    startService(Intent(this, HitroService::class.java))
   }

Upvotes: 0

Vlad Tsepelev
Vlad Tsepelev

Reputation: 2046

Use a Context.startService() method.

And read this.

Upvotes: 1

Vihaan Verma
Vihaan Verma

Reputation: 13143

Add this in your code

Intent serviceIntent = new Intent(this, ServiceName.class);
    startService(serviceIntent);

Dont forget to add service tag in AndroidManifest.xml file

<service android:name="com.example.ServiceName"></service>

From the Android official documentation:

Caution: A service runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service.

Upvotes: 61

Goutam
Goutam

Reputation: 21

First create service from android Manifest.xml file (i.e from application tab) and give some name to it.
On the activity on some event like click or touch to include the code from the service:

public void onClick(View v)
{
    startService(new Intent(getApplicationContext(),Servicename.class));
}

If you want to stop the running or started service then include this code:

public void onclick(View v)
{
    stopService(new Intent(getApplicationContext,Servicename.class));
}

Upvotes: 2

Pierre-Antoine LaFayette
Pierre-Antoine LaFayette

Reputation: 24402

The application can start the service with the help of the Context.startService method. The method will call the onCreate method of the service if service is not already created; else onStart method will be called. Here is the code:

Intent serviceIntent = new Intent();
serviceIntent.setAction("com.testApp.service.MY_SERVICE");
startService(serviceIntent);

Upvotes: 43

Josh Lee
Josh Lee

Reputation: 177574

The API Demos have some examples that launch services.

Upvotes: 1

Related Questions