The development and installation of remote service units for Android Java Written by:
Darkov Igor software developer team of devices, Apriori Inc
In this article I have described:
- How to develop a simple Java service for Android devices;
- How to communicate with other service process and a remote PC;
- How to install and start the service remotely from the PC.
1. Java Development Service for Android devices
Services are running long process background provided by Android. They could be used for execution of background tasks. Tasks may be different: basic calculations, backup procedures, Internet communications, services, etc. can be run on the system and applications can communicate with other processes using the technology channel Android IPC. The Android system can control the lifecycle of service based on customer requests, memory and CPU usage. Note that the service is a lower priority than any process that is visible to the user.
Let's develop the service simple example. It will show expected and requested notifications to the user. Service must be managed using the service request, press the simple activity and Android from the PC.
We first need to install and prepare the environment:
- Download and install the latest version of Android SDK on the official website (http://developer.android.com);
- Download and install Eclipse IDE (http://www.eclipse.org/downloads/);
- Also we will need to install Android Development Tools (ADT) plug-in for Eclipse.
After the environment is ready, we can create Eclipse Android project. It will include sources, resources, files generated and the Android manifest.
1.1 The Service Development Class
First we need to implement the service class. It should be inherited from the android. App.Service (http://developer.android.com/reference/android/app/Service.html) base class. Each class of service must have the corresponding statement <service> show his package. Manifest Declaration will be described later. Services, as objects of other applications running in the main thread of the process of accommodation. If you need to do intensive work, you should do so in another thread.
In the class of service that we should implement onBind abstract method. So we substitute other methods:
- onCreate (). It is called by the system when the service is created the first time. Usually, this method is used to initialize service resources. In our case, the binder, the task and timer objects are created. Also the notification is sent to the user and the system log:
public void onCreate () (super.onCreate (); Log.d (LOG_TAG, "Building Services"); showNotification ("Creating NotifyService"); binding = new NotifyServiceBinder (Manager Notificators); NotifyTask task = new (manager, Notificators) timer = new Timer ();)
- OnStart (Intent intent, int Startide). It is called by the system each time a client starts the service by calling explicitly startService (intent), providing the arguments it requires and the unique integer token representing the application for leave. We can start substantive discussions, tasks and perform other operations start.
Public void OnStart (Intent intent, int Startide) (super.onStart (intention, Startide); Log.d (LOG_TAG, "Starting service"); showNotification ("Starting NotifyService"); timer.scheduleAtFixedRate (task, Calendar.getInstance (). getTime (), 30000);)
- OnDestroy (). It is called by the system of notification of a position he no longer used and is removed. Here, we should perform all operations before the service is stopped. In our case we will stop all the tasks programmed timer.
OnDestroy public void ().
Posted on June 7, 2010.