How to start an application for Android applications only

I am creating an application whose only component is a service that continues to run in the background (mainly a proxy server), but I cannot find a way to start this service. An application cannot have any user interface or user interaction, so I do not use Activity.
Broadcast receiver can listen to the BOOT broadcast, but how do I get started the first time it is installed, and how can I start it? or is there a broadcast that I can listen to after the application is installed, for example. maybe TIME_TICK, but it should be logged from activity, I think.

+46
android service
Jun 13 '09 at 7:25
source share
3 answers

Unfortunately, there is currently no reliable way to receive a broadcast event after installing your application, ACTION_PACKAGE_ADDED . Intent does not translate to a newly installed package.

To receive the ACTION_BOOT_COMPLETED event , you need the broadcast receiver class as well as your service. I would also recommend adding ACTION_USER_PRESENT the intention to be caught by this broadcast receiver, this requires Android 1.5 (minSDK = 3), this will call your broadcast receiver when the user unlocks his phone. The last thing you can do to try and save your work without turning it off automatically is to call Service.setForeground () on your onCreate service to tell Android that your service should not stop, this was added mainly for services like mp3 - players that must continue to work, but can be used by any service.

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

Here is a simple class that you can use as a broadcast receiver for 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.

+42
Jun 13 '09 at 16:10
source share
— -

Return START_STICKY to Service.onStartCommand

Android will restart your service

+14
Jul 23 2018-10-23T00:
source share

I am exploring the same issue. Here is what I found: http://kfb-android.blogspot.de/2009/04/registering-for-timetick-after-reboot.html

This simple little demo demonstrates the solution by playing a little ping-pong between BroadcatReceiver and the service. The recipient is registered to start at boot time by registering in the manifest to receive ACTION_BOOT_COMPLETED. It just starts the service when it receives ACTION_BOOT_COMPLETED. The service, in turn, registers the BroadcastReceiver, and then for ACTION_TIME_TICK.

The application logic will be implemented in BroadcastReceiver.

0
May 30 '13 at 10:13
source share



All Articles