Home » Android » android – Is it possible to start a service with a shortcut?

android – Is it possible to start a service with a shortcut?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to create a shortcut on the home screen that, when pressed, will start a service instead of an activity.

Is it possible? How?

Thanks!

How to&Answers:

You can create a dummy Activity that simply starts a Service, then finishes itself:

public class MyServiceActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MyService.class);
        startService(intent);
        finish();
    }
}

Answer:

No, sorry. Shortcuts only launch activities.