How to Start Service-Only Android App

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.

Android application as a service without activity

Sure! No reason you cannot have an application with only a service. ...and no need to get into AIDL unless you want to.

The problem is, how to make the application run. When you create an application with an Activity, you add an Intent filter, in the manifest, that makes the activity startable from the Launcher. If there's no activity, you'll have to find another way to start it.

It is easy to do, though. Just fire an intent from one of your other programs, like this:

startService(new Intent("my.service.intent"));

... where the service is registered your manifest, like this:

        <service android:name=".SomeService" >
<intent-filter>
<action android:name="my.service.intent"/>
</intent-filter>

You could use that intent to pass Parcelable parameters to the service, and the service can reply by broadcasting intents back.

Of course startService and broadcastIntent are a bit clunky if you really need a complex API between applications and your service. If you need something richer, you will want to look into AIDL and a Bound Service.

Edited to add Intent Filter

How to make an Android app only run background services, and not be launcheable?

You said you didn't want to use a translucent Activity, but that seems to be the best way to do this:

1- In your Manifest, set the Activity theme to Theme.Translucent.NoTitleBar.

2- Don't bother with a layout for your Activity, and don't call setContentView().

3- In your Activity's onCreate(), start your Service with startService().

4- Exit the Activity with finish() once you've started the Service.

In other words, your Activity doesn't have to be visible; it can simply make sure your Service is running and then exit, which sounds like what you want.

how to hide luncher icon :

PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Hide open button couldn't be done , because we are talking about an application and not a google library

how to unhide icon :

PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

IMPORTANT: It's somehow tricky if you need to do something with main activity in your app when it's hidden. you will face an ActivityNotFoundException. to make it work, you should unhide icon before doing anything to your main activity and hide it again after you are finished.

Start Service at boot without activity

From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

Therefore in your application you must have one Activity to run BroadcastReceiver.

Start service in Android

Probably you don't have the service in your manifest, or it does not have an <intent-filter> that matches your action. Examining LogCat (via adb logcat, DDMS, or the DDMS perspective in Eclipse) should turn up some warnings that may help.

More likely, you should start the service via:

startService(new Intent(this, UpdaterServiceManager.class));

Android start service of an app from another android app

Not exactly a great solution but I have fixed my problem by removing the NotificationService file from the com.app.service directory, and adding it to the com.app directory.
This fixed the issue for me, meaning that I managed to start the service of the second application from the first application.

How to make my service only run in one instance in multiple user android system?

Yes, Need to declare app as system app. also use bindServiceAsUser() with System UserHandler.

How to launch a Android Service when the app launches?

Correct me if I am wrong, but android.intent.category.LAUNCHER is valid only for Activity. So, does not look like valid way to start Service. The same you can achieve if you do the following:

  • create transparent Activity that will be used only to start Service
  • for that Activity, you do not need to specify GUI layout. So, you do not need to setContentView() in the activity's onCreate(). The only thing you need is to put

@android:style/Theme.NoDisplay

under Theme tag for this Activity in AndroidManifest.xml.

  • start Service from onCreate() of your Activity.
  • call finish() in onStart() of your Activity to close it.

So, your Activity will be invisible to the user, last shortly and nobody will notice that it was used to start the service.



Related Topics



Leave a reply



Submit