How to Start Android Service on Installation

How to start android service on installation

You cannot do this -- there is no way to automatically start your service merely because it was installed.

The application must first be invoked by the user through some sort of activity. Or, you are going to need to hook into some relevant broadcast Intent via the manifest, so you can get control when one of those events occur and kick off your service that way. Or, you are going to need to ask the user to reboot so your BOOT_COMPLETED Intent filter can get control.

Start a service automatically when app installed to device

Google shows the correct answer as the first hit so ... have you done some research on this?
How to start a Service when .apk is Installed for the first time

Summary: you can't do this.

Start service on installation of app without any activity

There is no straightforward way to start a service on just installing the app, without user opening it first time. If you read the answers on the above two questions, you will get the gist.

1) How to start a Service when .apk is Installed for the first time

2) How to start android service on installation

Hope it helps you.

How to start Service-only Android app

Unfortunately right now there is no reliable way to receive a broadcast event after your applicaiton has been installed, the ACTION_PACKAGE_ADDED Intent does not broadcast to the newly installed package.

You will have to have a broadcast receiver class as well as your service in order to receive the ACTION_BOOT_COMPLETED event. I would also recommend adding the ACTION_USER_PRESENT intent to be caught by that broadcast receiver, this requires Android 1.5 (minSDK=3), this will call your broadcast receiver whenever the user unlocks their phone. The last thing that you can do to try to keep your service running without having it easily be shut down automatically is to call Service.setForeground() in your service onCreate to tell Android that your service shouldn't be stopped, this was added mainly for mp3 player type services that have to keep running but can be used by any service.

Make sure you add the proper permissions for the boot_complete and user_present events in you manifest.

Here is a simple class that you can use as a broadcast receiver for the events.

package com.snctln.util.WeatherStatus;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class WeatherStatusServiceReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction() != null)
{
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ||
intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
context.startService(new Intent(context, WeatherStatusService.class));
}
}
}
};

Good luck.

How to start a Service when .apk is Installed for the first time

Fortunately, Plan B does not work on Android 3.1+, as tested on a XOOM and a Galaxy Nexus.

What Plan B does is exploit a security hole that could be used by drive-by malware, which is specifically why Android prevents it from happening anymore.


UPDATE

To clarify: As inazaruk posted and I put into comments on other answers, all applications, upon installation, are placed in a "stopped" state. This is the same state that the application winds up in after the user force-stops the app from the Settings application. While in this "stopped" state, the application will not run for any reason, except by a manual launch of an activity. Notably, no BroadcastReceviers will be invoked, regardless of the event for which they have registered, until the user runs the app manually.

This block covers the Plan B scenario of remote-install-and-run, which they were taking advantage of previously. After all, with that, anyone with a hacked Google account would be at risk of having their device infected, hands-free as it were.

So, when the OP says:

I need to start a Service as soon as the Applicaton gets installed on the Device

the OP will be unsuccessful and will need to redesign the application to avoid this purported "need".

Service registration during APK installation

Nevermind, I discovered a solution a day after I asked the question.

If you ever encounter such a problem, for some reason you need to start the service like this:

Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.setClassName("com.example.myapp", "com.example.myapp.MyService"); // add this line
startService(intent);

I don't know why this happens, the intent seems to be identical. When I print intent.toString() to the log before and after I call intent.setClassName, both lines look like this:

Intent { cmp=com.example.myapp/.MyService }

If anyone knows the reason for this behavior, please share it with me, as I have no idea why this happens.



Related Topics



Leave a reply



Submit