»
S
I
D
E
B
A
R
«
http proxy export
April 5th, 2010 by admin

http proxy export

Development and installation of Java remote service for Android devices

Written by:
Igor Darkov, the developer of computer software
devices, Apriorit Inc.

In this article I have described:

  • How develop simple Java service for Android devices;
  • How to communicate with a service of the other processes and a remote PC;
  • How install and start the service remotely from the PC.

1. Java Development Service for Android devices

Services are long-standing background processes provided by Android. They can be used for tasks running in the background. Tasks may be different background calculations, procedures Backup, Internet communications, services, etc. can start your system and applications can communicate with other processes using IPC channels Android technology. The Android system can control the lifecycle of service according to customer requests, memory usage and CPU. Note that the service has a lower priority than any process that is visible to the user.

We will develop the service simple example. Scheduled to be shown and asked to notifications to users. Service must regulated by the service request, inform the Android simple activity and from the PC.

First we have to install and prepare the environment:

  • Download and install the latest Android SDK's official website (http://developer.android.com)
  • Download and install Eclipse IDE (http://www.eclipse.org/downloads/)
  • We will also need to install Android Development Tools (ADT) plug-in for Eclipse.

After the environment is ready Android can create Eclipse project. It will include the sources, resources, Android generated files and the manifest.

1.1 Development of class services

First we have to apply the kind of service. It must be inherited from the android.app.Service (http://developer.android.com/reference/android/app/Service.html) base class. Each class of service must have a corresponding declaration <service> on the manifest of your package. Manifesto statement described below. Services, like objects from another application, running on the main thread of its host process. If you need to do some intensive work, you must do so in another thread.

In the class of service to be applied onBind abstract method. Also override some other methods:

  1. onCreate (). Called by the system when the service is created in the first time. Generally, this method is used to initialize service resources. In our case the binder, the task and objects created timer. Also notification will be sent to the user and the system log:

onCreate public void () (super.onCreate (); Log.d (LOG_TAG, "Building services"); showNotification ("Creating NotifyService") binding = new NotifyServiceBinder (driver, notificator) NotifyTask task = new (controller, notificator) counter = new Timer ();)

  1. OnStart (Intent intent, int start). It is called by the system whenever a client explicitly starts the service by calling startService (Intention), providing the arguments it needs and the unique integer symbol that represents the application startup. You can start substantive discussions, schedule tasks, and other startup operations.

Public void OnStart (intention of Intent, int start) (Super.onStart (intention, start); Log.d (LOG_TAG, "Starting service"); showNotification ("Starting NotifyService"); timer.scheduleAtFixedRate (task, Calendar.getInstance (). GetTime (), 30000);)

  1. OnDestroy (). Called by the system to notify a service that no longer use and disposal. Here we perform all operations before the service stops. In our case we will stop all scheduled tasks timer.

OnDestroy public void () (super.onDestroy (); Log.d (LOG_TAG, "Stop service"); showNotification (NotifyService Stop "); timer.cancel ();)

  1. onBind (Intent intent). Refund of communication channel with the service. IBinder is the basic interface specific for remotable object, the essence of a lightweight mechanism called remote procedure. This mechanism is designed for high performance production in-process and inter-process calls. This interface describes the summary protocol to interact with an object remotely usable. IBinder application described below.

onBind public IBinder (Intent intent) (Log.d (LOG_TAG, "Service Link") return binding;)

To send exit registration system you can use static class methods android.util.Log (http://developer.android.com/reference/android/util/Log.html). For browse the system logs on the PC you can use the utility command ADB: Asian Development Bank logcat.

The notification function is performed in our service as special executable object. It could be used in the other threads and processes. The service class has showNotification method, which can display message the user using the call Toast.makeText. The executable object is also used:

NotificationRunnable public class implements Runnable (String private message = null; public void run () (if (null! message =) (showNotification (message);)) public void setMessage (String message) (message = this.message;))

The code thread running on the service. To run executable method can be used android.os.Handler special object. There are two main uses for the controller: to schedule Runnable messages and running as some point in the future, and to place an action to be performed on a thread different from yours. Each example is associated driver with a single thread and the thread message queue. To display the notification shall establish and publish the pages call () method of the Handler object.

1.2 CIP Service

Each application runs in its own process. Sometimes you need to pass objects between processes and call some methods of service. These operations can be performed using the CIP. In the Android platform, a process that normally can not access the memory of another process. So they have to break down in the primitive objects that can be understood by the operating system, and "marshall" the object through this limit for developers.

The mechanism of digestible lysine Apparent ileal CIP is used in Android devices. Based interface is similar to COM or Corba, but is lighter. Use a proxy class to pass values between the client and implementation.

AIDL (Android Interface Definition Language) IDL is a language used to generate the code that allows two processes Android device to communicate using IPC. If you have the code in a process (eg, action) that is to call the methods of the object of another process (Eg, Service), can be used to generate code AIDL Marshall parameters.

Service interface example shown sendNotification below only supports one call:

INotifyService interface (sendNotification void (String message);)

IBinder interface for an object is usable remote is used by customers for the IPC. The client can contact the service by calling bindService context (). The application may be found in IBinder onBind method. The implementation of interface INotifyService android.os.Binder is based on class (http://developer.android.com/reference/android/os/Binder.html):

NotifyServiceBinder public class extends implements INotifyService Binder (private driver controller = null; private notificator NotificationRunnable = null; public NotifyServiceBinder (Handler handler, notificator NotificationRunnable) (this.handler = driver; this.notificator = notificator;) public void sendNotification (String message) (if (null! = Notificator) (notificator.setMessage (message); handler.post (notificator))) public IBinder asBinder () (return this;))

As described above, notifications can be sent using the post object Handler () method call. NotificaionRunnable object passed as a parameter of the method.

By the client side you may be asked IBinder object and work with it as INotifyService interface. To connect to the service interface implementation android.content.ServiceConnection can be used. Two methods must be defined: onServiceConnected, onServiceDisconnected:

ServiceConnection conn = null; … conn = new ServiceConnection () (public void onServiceConnected (ComponentName name, service IBinder) (Log.d (NotifyTest "," onServiceConnected "); INotifyService s = (INotifyService) services; try (s.sendNotification ("Hello")) catch (RemoteException ex) (Log.d (NotifyTest "," Unable to send notification " ex);)) public void onServiceDisconnected (ComponentName name) ());

BindService The method can be called from the context of the client to connect to business service:

Context.bindService (new Intent (this, NotifyService.class), conn, Context.BIND_AUTO_CREATE)

The method can be called from unbindService the context of client activity to disconnect the service:

Context.unbindService (conn);

1.3 Remote Service

Transmissions are the form of applications and system components can communicate. We may also use the service control emissions from the PC. Messages are sent as Purpose, and the system manages the shipment of them, including the beginning of the receivers.

Purposes may be issued to BroadcastReceivers, allowing messaging applications. By registering a BroadcastReceiver in AndroidManifest.xml application (labeled <receiver>) you can take your application receiver class in progress and called each time someone sends a broadcast. Activity Manager uses the IntentFilters, applications register to find out which program to use for a program determined.

We will develop the receiver to be notified to start and stop services on request. The base class android.content.BroadcastReceiver be used for these purposes (http://developer.android.com/reference/android/content/BroadcastReceiver.html):

public static class BroadcastReceiver START_ACTION extends ServiceBroadcastReceiver … (Private String = "NotifyServiceStart" STOP_ACTION private static String = "NotifyServiceStop" public void … OnReceive (Context context, the intention of Intent) (String action = intent.getAction … () if (START_ACTION.equalsIgnoreCase ((action)) context.startService (new Intent (context, NotifyService.class))) else if (STOP_ACTION.equalsIgnoreCase ((action)) context.stopService (another attempt (context, NotifyService.class ));)))

A Post transmitted from the client application that uses the call Context.sendBroadcast. I will describe how to use the receiver and send transmissions from the PC in Chapter 2.

Android 1.4 Manifesto

Each application must have a file in your root directory AndroidManifest.xml. The manifest contains information about the application Android system, the system must have this information before it can execute any application code. The basic components of an application (its activities, services and broadcast receivers) are activated intentions. An attempt is a package of information (an object Intent) that describes an action desired – including data were taken into account, the category of component must perform the action, and in other directions. Android locates a appropriate component to meet the intent, it starts the new instance of the component if necessary, and passes the object Intent.

2 We must describe components of our service:

  • NotifyService class is described on the label <service>. Not be initiated on the intention. So the intention filtering is not necessary.
  • ServiceBroadcastReceived class is described on the label <receiver>. For the broadcast receiver using the filter intended select the system events:

android: icon = "@ drawable / icon" android <application android:label="@string/app_name"> service … <: Enabled = "true" android: name = ". NotifyService" android: exported = "true"> </ <receiver> Service android: name = "ServiceBroadcastReceiver"> <intent-filter> <action android:name="NotifyServiceStart"> </ action> <action android:name="NotifyServiceStop"> </ action> </> filter <intención /> receiver …

2. Installation Java remote service and start

2.1 Services Installation

Services and other applications for the Android platform can be installed from the package especially with the APK. extension. Android package contains all necessary binaries and manifest.

Before installing the PC service should allow to the USB debug option in the Settings-Device Development Applications menu and then connect the device to the PC through USB.

By the side of PC going to use utility ADB, which is available in the Android SDK tools directory. ADB Utility supports several optional arguments of the command line provided powerful features, such as copying files to and from the device. The argument command line shell lets you connect to the phone itself and rudimentary shell issue commands.

We will use several commands:

  • remote shell command execution: "adb shell <command> <arguments>
  • Operation of transmission of files: "adb push path> Local> <remote path>
  • operation installation package "adb install package. APK

I will describe the installation process for package details. It consists several steps that are performed by the utility installation command ADB:

  • First of all file. APK package should be copied to the device. The utility ADB is connected to the device and merely "shell" user privileges. So almost all the file system directory is write-protected it. The / data / local / tmp is used as temporary file storage package. To copy the package into the device, use the command:

push NotifyService.apk adb / Data / local / tmp

  • Installation package. BAD utility uses special shell commands to perform this operation. The "pm" (Manager package?) Utility is present in Android devices. It supports multiple command line parameters described in Appendix I. To install the package itself in the remote shell command:

Adb shell pm install / data / local / tmp / NotifyService.apk

  • Cleanup. After installing the package, the ADB removes temporary files stored in / data / local / tmp directory using the "rm" utility:

rm adb shell / data / local / tmp / NotifyService.apk.

  • To uninstall package using the "pm" utility:

Adb shell pm uninstall <package>

2.2 Remote Service

To start and stop the NotifyService from the PC can use the "am" (Activity Manager?) Utility that is present in the Android device. The command line parameters are described in Appendix II. The "am" utility can send attempts the broadcast system. Our service is the recipient of which will be launched at the request of the system.

To start we can execute commands NotifyService remote shell:

Adb shell am diffusion-A NotifyServiceStart

To stop NotifyService can execute remote shell commands:

Adb shell dissemination, I'ma NotifyServiceStop

Note that the NotifyServiceStop NotifyServiceStart and intentions described in the manifest file inside the tag … <receiver> <intent-filter>. Other applications will not start the receiver.

Appendix I. PM of use (Android console)

pm [list | | Path Installation | remove [] list packages of hours-f] pm list permission permissions list groups pm [G] [-f] [-d] [-u] [GROUP pm] path installation pm package [l] [-r] pm RUTA remove [-k PACKAGE] The list command prints all packages packages. Use the-f option to see their associated files. The command list-permission groups prints all the known permission groups. The permit will list all permits known prints, optionally, only those in the group. Use the-g option to organize groups. Use the-f option to print all information. Use the-s option of a brief summary. Use the-d option to list only permits dangerous. Use the option-ua the list only permits users will see. The path command prints the path to the APK. Of a package. The command installs a package on the system. Use the-l FORWARD_LOCK install package. Use the-r option to reinstall an existing application, keeping your data. The uninstall command removes a package from the system. Use the option-k to keep the data and cache directories around after package removal.

Appendix II. AM use (Android console)

am [Home | publication | instrument] am start-D AM broadcast INTENT INTENTION am instrument [-r] [-e <ARG_NAME> <ARG_VALUE>] [-P <PROF_FILE>] [-W] <component> intention is described: [-a <action>] [-d <DATA_URI>] [-t <MIME_TYPE>] [-c <category> [-C <category>] …] [-e | - is <EXTRA_KEY> <EXTRA_STRING_VALUE> ...] [- ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ... []-E | – ei <EXTRA_KEY> <EXTRA_INT_VALUE> …] <component> [-n] [-f <flags>] [<uri>]

Resources:

  • Android Installation Guide.

http://developer.android.com/sdk/1.5_r2/installing.html

  • Android Developer Reference.

http://developer.android.com/reference/classes.html

  • Jesse Burns. Mobile Application Development Android.

https: / / www.isecpartners.com / files / iSEC_Securing_Android_Apps.pdf

  • The design of a remote interface through lysine apparent ileal digestible

http://developer.android.com/guide/developing/tools/aidl.html

About the Author

Apriorit is an Ukrainian software development company.

Apriorit develops its own products as well as provide offshore development and QA services in the areas of advanced system programming, driver development, software for devices.

One of the key values of Apriorit’s specialists is knowledge generation and sharing of experience.

Learn more about Apriorit and its experience at Apriorit Official site

Zaid Hamid: Pak Export of Terrorism


Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Leave a Reply

»  Substance: Play Roulette   »  Style: Ahren Ahimsa