Android callRemoteService calls services of other processes

Source: Internet
Author: User

Create a process service. The interface is not required when the program is run for the first time.

If you want other processes to call this process across processes. you must use the interface type for cross-process access. you only need to remove the modifiers of classes and methods in the interface. then, program the interface class to the aidl type.

Service also business code:


[Java]
Package com. example. remoteservice;
 
Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Import android. OS. RemoteException;
 
/**
* Cross-process access interface type: remove the modifier types of interfaces and interfaces,
* Steps for android to generate an aidl file: Find the written java source file, set its extension to aidl, and return to the Project
*, Refresh the project, the class will change to the file named. aidl in the source file, and the system will automatically
*. The project directory is a java file generated with the name of the original java file. You can open it and check that the content is different from the previous one. At this point, aidl is created.
*
* @ Author Administrator
*
*/
Public class RemoteService extends Service {
 
@ Override
Public IBinder onBind (Intent arg0 ){
Return new Mybinder ();
}
 
/**
* The IServcice. Stub class after iservice. adil can be generated by the system based on the defined iservice interface,
* Stub is stored in the system's own home. This class is an abstract class that inherits the binder class and implements iservice. <br>
*
* @ Author Administrator
*
*/
Public class Mybinder extends IServcice. Stub {
 
@ Override
Public void callMethodInService () throws RemoteException {
SayHelloInService ();
}
 
}
 
/**
* Methods in the service
*/
Public void sayHelloInService (){
System. out. println ("hello in service ");
}
 
@ Override
Public void onCreate (){
System. out. println ("remote service create ");
}
 
}
The service process provides an interface called externally and converts the interface into an aidl file,
Aidl File Creation: Find the written java source file and set its extension to aidl. return to the project, refresh the project, and the class will change to the source file name. aidl file, and the system will automatically go to gen. the project directory generates the java file with the name of the original java file. the content is different from the previous one. now the aidl is created.


[Html]
Package com. example. remoteservice;
 
Interface IServcice {
Void callMethodInService ();
}

Register the service in the configuration file application

[Html]
<Service android: name = ". RemoteService">
<Intent-filter>
<Action android: name = "com. example. remoteservice"/> <! -- Specify action to facilitate calls by other programs -->
 
</Intent-filter>
</Service>

The called service has been created. if you want to use it externally. the process must be started. and services. so. in combination with the example called below (the service that calls an external process), the process must run normally.
Summarize the above methods for calling services in this process:

1. Create a service that must contain methods that can be called;
2: Define an excuse for IService. abstract methods in the interface call methods in the service;
3: Define a MyBinder object. inherit the binder class, implement the IService interface, and return the MyBinder object in the onBind method;
4: bindService () is used to bind a service in the activity;
5: Create a MyConn interface to implement ServiceConnection. After the service is successfully bound, run the onServiceConnected () method in the interface;
6: The IBinder parameter of the onServiceConnected () method is of the IService type and is forcibly converted to IService;
7. Call methods in ISservice

Call services in external processes (the process above)

To access services in other processes:

Access external services. you need to know the action of the other service, the adil file that can call the service interface, and the package name of the adil file. create a package for calling the aidl file of the service in a local program, and add (copy) The aidl file of the external service. The system will automatically create the java file of the adil file interface under the gen directory.

1. Create a service that must contain methods that can be called;
2: Define an excuse for IService. The abstract methods in the interface call the methods in the service, remove the modifier defining the interfaces and the methods in the interfaces, and set the extension of this interface to. aidl ,;
3: Define a MyBinder object and inherit the Sutb class in the IService class created in gen by the compiler according to IService. aidl,
Returns the MyBinder object in the onBind method.
4: Create a package in the project that calls the service. The package name must be the same as the package name of the aidl file in the called service project. copy the aidl file corresponding to the called Project to the local aidl file package.
5: bindService () is used to bind a service in the activity;
6: Create a MyConn interface to implement ServiceConnection. After the service is successfully bound, execute the method in the interface;
7. Call the IServcice. Stub. asInterface () method in the onServiceConnected () method in the implementation interface. Convert the IBinder parameter of onServiceConnected to the IService type;
8: Call methods in IService (call methods in the service ),

 

After creating the same package and copying the called aidl file according to the above steps, create a layout for calling the external process page. In this example, only one button is used as an example.


[Html]
<Button
Android: id = "@ + id/call"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "click"
Android: text = "calling external services"/>


Code implementation for calling external services

[Java]
Package com. example. callremoteservice;
 
Import com. example. remoteservice. IServcice;
 
Import android. app. Activity;
Import android. content. ComponentName;
Import android. content. Context;
Import android. content. Intent;
Import android. content. ServiceConnection;
Import android. OS. Bundle;
Import android. OS. IBinder;
Import android. OS. RemoteException;
Import android. view. View;
 
/**
* Access the external service. You need to know the action of the other service, the adil file that can call the service interface, and the package name of the adil file <br>
* Create a package for calling the adil file of the service in a local program and add the adil file of the external service. The system will automatically create the java file for the adil file interface in the gen directory. <br>
* Conclusion: <br>
* The bindService () method is required to access the service in this process. (cz23_serverlifecycle) <br>
*
* 1. Create a service that must contain methods that can be called. <br>
* 2: Define an excuse for IService. abstract methods in the interface call methods in the service. <br>
* 3: Define a MyBinder object. inherit the binder class and implement the IService interface. The MyBinder object is returned in the onBind method. <br>
* 4: bindService () is used to bind a service in the activity. <br>
* 5: Create a MyConn interface to implement ServiceConnection. After the service is successfully bound, run the onServiceConnected () method in the interface. <br>
* 6: The IBinder parameter of the onServiceConnected () method is of the IService type and is forcibly converted to IService. <br>
* 7: Call methods in ISservice <br>
* <Br>
* Aidl (android interface definition language) is required to access the service in an external process (two examples numbered 24) <br>
* 1. Create a service that must contain methods that can be called. <br>
* 2: Define an excuse for IService. The abstract methods in the interface call the methods in the service, remove the modifier of the methods defined in the interface and the interface, and set the extension of this interface. aidl,; <br>
* 3: Define a MyBinder object and inherit the Sutb class in the IService class created in gen by the compiler according to IService. aidl,
* Returns the MyBinder object in the onBind method. <br>
* 4: Create a package in the project that calls the service. The package name must be the same as the package name of the aidl file in the called service project. copy the aidl file corresponding to the called Project to the local aidl file package. <br>
* 5: bindService () is used to bind a service in the activity. <br>
* 6: Create a MyConn interface to implement ServiceConnection. After the service is successfully bound, execute the methods in the interface. <br>
* 7: The onServiceConnected () method in the implementation interface calls the IServcice. Stub. asInterface () method.
* Convert the IBinder parameter of onServiceConnected to the IService type. <br>
* 8: Call methods in IService (call methods in the service ),
*
* @ Author Administrator
*
*/
Public class MainActivity extends Activity {
Private IServcice iServcice;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Intent intent = new Intent ();
Intent. setAction ("com. example. remoteservice"); // implicit intent, specifying the action to access the service is the value of the action specified when the service process value is called to register the service
BindService (intent, new MyConn (), Context. BIND_AUTO_CREATE); // bind to an external service by implicit intent. parameter 2 is a callback class after the service is successfully bound. attackers can obtain external data objects from external services.
}
 
Public void click (View view ){
Try {
IServcice. callMethodInService ();
} Catch (RemoteException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
 
Class MyConn implements ServiceConnection {
 
@ Override
/**
* The IBinder parameter is the return value of the onBind bound to the called service.
*/
Public void onServiceConnected (ComponentName arg0, IBinder service ){
IServcice = IServcice. Stub. asInterface (service); // call the service conversion method in an external process
}
 
@ Override
Public void onServiceDisconnected (ComponentName arg0 ){
// TODO Auto-generated method stub
 
}
 
}
}

 

 

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.