Android layered learning notes (4)

Source: Internet
Author: User

Write the underlying library, and then write the application for use or verification.
As mentioned above, applications can access the JNI library in multiple ways. The simplest way is to directly call the JNI library, the second is to use the service, and the second is to use the service manager.
1. Load directly.
This is a bit like jumping down from the third floor. Actually, Java has an interface that calls native code. Android development also includes ndk Development, which is to directly use C for applications.

1.1 testjni1.java
In the same directory as framwork, create the app directory, mkdir app
Create the dirload/src/COM/ask/directory under the app directory and generate testjni1.java. The content is as follows:

Package com. Ask. testjni;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;
Public class testjni1 extends Activity
{

Static
{
// System. Load ("/system/lib/libaskgpio. So ");
System. Load ("/system/lib/libaskgpio ");
}

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// Call an API on the library.
_ Gpio_init ();
_ Gpio_set (1 );
_ Gpio_clr (2 );

Textview TV = new textview (this );
TV. settext ("gpio 1 is set to 1. gpio 2 is set to 0 .");
Setcontentview (TV );
}
Private Static native Boolean _ gpio_init ();
Private Static native Boolean _ gpio_set (INT gpio );
Private Static native Boolean _ gpio_clr (INT gpio );
}
Note: system. load ("/system/lib/libaskgpio"); in actual tests, loading may fail. load ("/system/lib/libaskgpio. so ");

1.2 write Android. mk and save it in the dirload directory

Local_path: = $ (call my-DIR)

Include $ (clear_vars)

Local_module_tags: = user

# This is the target being built.
Local_package_name: = testjni1

# Only compile source Java files in this APK.
Local_src_files: = $ (call all-Java-files-under, Src)

# Link against the current Android SDK.
Local_sdk_version: = Current

Include $ (build_package)

1.3 write androidmanifest. xml and save it to the dirload directory.
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Ask. testjni1">
<Application>
<Activity Android: Name = "testjni1">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>

Finally, the testjni1.apk file will be generated in out/target/product/generic/system/APP.

2. Call the service method
2.1 create the serviceload/src/COM/ask/directory under the app directory and generate serviceload. java. The content is as follows:
Package com. Ask. serviceload;
Import com. Ask. server. serviced; // note that it is not gpioservice
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;

Public class serviceload extends activity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// Call an API on the library.
Serviced SD = new serviced ();
SD. gpioset (1 );
SD. gpioclr (2 );
Textview TV = new textview (this );
TV. settext ("gpio 1 is set. gpio 2 is CLR .");
Setcontentview (TV );
}
}
Here, a new serviced will load the JNI library, and then call the API function gpioset and gpioclr in serviced.
2.2 compile the Android. mk file and save it to the serviceload directory.
Local_path: = $ (call my-DIR)

Include $ (clear_vars)

Local_module_tags: = user

# This is the target being built.
Local_package_name: = serviceload

# Only compile source Java files in this APK.
Local_src_files: = $ (call all-Java-files-under, Src)

# Link against the current Android SDK.
Local_sdk_version: = Current

# Also link against our own m library.
Local_java_libraries: = ask

Include $ (build_package)

2.3 write androidmanifest. xml and save it to the serviceload directory.
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Ask. gpioclient">

<Application>

<! -- This tells the system about the custom library used by
Application, so that it can be properly loaded and linked
To the app when the app is initialized. -->
<Uses-library Android: Name = "com. Ask. Serviced"/>

<Activity Android: Name = "serviceload">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>

Finally, the serviceload.apk file will be generated in out/target/product/generic/system/APP.

2.4 because the serviceload class is used to run this program, add a permission under/etc/permission, as shown below:
<? XML version = "1.0" encoding = "UTF-8"?>
<Permissions>
<Library name = "com. Ask. Serviced"
File = "/system/framework/ask. Jar"/>
</Permissions>
The file name can be com. Ask. serviced. xml.

3. Call JNI through Service Manager
3.1 create the managerload/src/COM/ask/directory under the app directory
3.1.1 generate the gpio system service class with the file name gpiosystemserver. Java
This class is generated mainly for managerload calls. The content is as follows:
Package com. Ask. gpiosystemserver;

Import com. Ask. server. gpioservice;

Import Android. OS. ibinder;
Import Android. OS. servicemanager;
Import Android. util. log;
Import Android. App. Service;
Import Android. content. context;
Import Android. content. intent;

Public class gpiosystemserver extends Service {

@ Override
Public ibinder onbind (intent ){
Return NULL;
}

Public void onstart (intent, int startid ){
Log. I ("gpiosystemserver", "Start gpioservice ...");

/* Please also see systemserver. Java for your interests .*/
Gpioservice GS = new gpioservice ();

Try {
Servicemanager. addservice ("gpio", GS );
} Catch (runtimeexception e ){
Log. E ("gpiosystemserver", "Start gpioservice failed .");
}
}
}
3.1.2 generate managerload. Java with the following content:
Package com. Ask. managerload;
Import ask. Hardware. gpiomanager;
Import com. Ask. server. gpioservice;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. widget. textview;
Import Android. widget. Button;
Import Android. content. intent;
Import Android. View. view;

Public class managerload extends activity implements view. onclicklistener {
Private gpiomanager mgpiomanager = NULL;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// Start ledservice in a seperated process.
Startservice (new intent ("com. Ask. systemserver "));

Button BTN = new button (this );
BTN. settext ("click to turn gpio 1 Set 1 ");
BTN. setonclicklistener (this );

Setcontentview (BTN );
}

Public void onclick (view v ){

// Get ledmanager.
If (mgpiomanager = NULL ){
Log. I ("ledtest", "creat a new mgpiomanager object .");
Mgpiomanager = new gpiomanager ();
}

If (mgpiomanager! = NULL ){
Log. I ("managerload", "Got gpiomanager object .");
}


Mgpiomanager. gpioset (1 );
Textview TV = new textview (this );
TV. settext ("gpio 1 is set 1 .");
Setcontentview (TV );
}

}
3.3 compile the Android. mk File
Local_path: = $ (call my-DIR)
Include $ (clear_vars)

Local_module_tags: = user

# This is the target being built.
Local_package_name: = managerload

# Only compile source Java files in this APK.
Local_src_files: = $ (call all-Java-files-under, Src)

# Link against the current Android SDK.
Local_sdk_version: = Current

# Also link against our own m library.
Local_java_libraries: = Ask framework

# We need to assign platform key to use servicemanager. addservice.
Local_certificate: = Platform

Include $ (build_package)

3.4 add an XML file under/etc/permission, just like 2.4.
Because the serviceload class is used to run this program, you must add a permission under/etc/permission, as shown below:
<? XML version = "1.0" encoding = "UTF-8"?>
<Permissions>
<Library name = "com. Ask. Server"
File = "/system/framework/ask. Jar"/>
</Permissions>
The file name can be com. Ask. gpioservice. xml.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.