Launch an Android application without the main operation and start the service when the application starts

I have a script in my application. My application does not have a user interface, instead there is a Service that starts at boot and will run continuously.

How to set up a manifest file without the main action? Can I run the application without any action? And when starting my application, my Service should start. Is this possible anyway? I do not want to do translucent actions to start the service.

Thanks in advance!

+55
android android-activity android-service
Jun 06 '12 at 7:11
source share
3 answers

You said you don't want to use translucent activity, but this is the best way to do this:

  • In the manifest, set the theme "Activity" to Theme.Translucent.NoTitleBar .
  • Do not worry about the layout of your activity and do not call setContentView() .
  • In your onCreate() activity, start the service using startService() .
  • Exit using finish() after starting the Service.

In other words, your activity should not be visible; he can just make sure your service is running, and then exit, that sounds the way you want.

I highly recommend showing at least a toast notification indicating to the user that you are starting the Service, or that it is already running. The very poor user interface has a launcher icon that does nothing when clicked.

+87
Jun 06 '12 at 7:14
source share

Yes, you can do this simply by creating a BroadcastReceiver that calls your Service when your application loads. Here is the complete answer given by me. Android - start service at boot

If you do not need any icon / launcher for the Application, you can also do this, just do not create any actions with

 <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 

Just declare your Service , as usual.

+22
Jun 06 '12 at 7:15
source share

Android Studio version 2.3

You can create a Service without the main activity by following a few simple steps. You can install this application through Android Studio and debug it like a regular application.

First create a project in Android Studio with no activity. Then create your service class and add it to your AndroidManifest.xml

 <application android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name="com.whatever.myservice.MyService"> <intent-filter> <action android:name="com.whatever.myservice.MyService" /> </intent-filter> </service> </application> 

Now, in the drop-down list next to the "Run" button (green arrow), go to "Edit Configurations" and in the "Launch Options" select "Nothing." This will allow you to install your service without Android Studio, complaining of a lack of core activity.

After installation, the service will NOT start, but you can start it with this adb shell command ...

 am startservice -n com.whatever.myservice/.MyService 

You can check it with ...

 ps | grep whatever 

I have not tried it yet, but most likely Android Studio will automatically start the service. This will be done in this menu “Edit Configurations”.

0
Oct 19 '17 at 23:57 on
source share



All Articles