Android AIDL and hanging up the phone

Source: Internet
Author: User

What is AIDL of Android? AIDL is the definition Language of Android Interface. To put it bluntly, it is defined by Android and a mechanism for inter-process communication,

It is a bit like the method that calls the Service.

Okay. The following code is used to describe

First, write an Interface

Com. xiaobin. service. IService

package com.xiaobin.service;public interface IService{public void sayService();}

It's very simple. It's just a method. It's the same as calling the methods in the Service as we mentioned earlier,

Now let's talk about the difference. Now we need to remove the public modifier of the method in the interface above, and then change the suffix to. aidl.

Then, we will find that Eclipse will generate a class for us.


Next, we will write another Service class

Com. xiaobin. service. remote. RemoteSrevice

Package com. xiaobin. service. remote; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. OS. remoteException; import com. xiaobin. service. remote. IService; public class RemoteService extends Service {@ Overridepublic IBinder onBind (Intent intent) {// return our own Binderreturn new MyBinder () ;}@ Overridepublic void onCreate () {System. out. println ("service onCreate"); super. onCreate ();} // public void sayService () {System. out. println ("I am another method in the process");} // inherit the abstract class MyBinder extends IService generated by AIDL. stub {@ Overridepublic void invokeServiceMethod () throws RemoteException {// call the method sayService () ;}} in the service ();}}}

After writing this, We will register it in AndroidMainfest.

        <service android:name="com.xiaobin.service.remote.RemoteSrevice">            <intent-filter>                <action android:name="com.xiaobin.remoteservice"/>            </intent-filter>        </service>

As you can see, we have specified an action, which is specified for future calls.

Okay, so far our remote service has been written.


Next, we will create a new project. Note that we will create a new project and then write a class that calls the service we just wrote.

Before writing, we need to copy the aidl file we just wrote to this project.



Then, let's write down our call logic.

Com. xiaobin. aidlservice. MainActivity

Package com. xiaobin. aidlservice; import android. app. activity; import android. content. componentName; import android. content. intent; import android. content. serviceConnection; import android. OS. bundle; import android. OS. IBinder; import android. OS. remoteException; import android. view. view; import android. widget. toast; import com. xiaobin. service. remote. IService; public class MainActivity extends Activity {private I Service iService = null; private MyServiceConnection conn; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. setContentView (R. layout. main); conn = new MyServiceConnection (); Intent intent = new Intent (); // The intent you just specified when registering the service. setAction ("com. xiaobin. remoteservice "); this. bindService (intent, conn, BIND_AUTO_CREATE);} public void click (View v) {try {if (iService! = Null) {// call the iService method in the interface. invokeServiceMethod ();} else {Toast. makeText (this, "An error occurred while calling the remote service", Toast. LENGTH_SHORT ). show () ;}} catch (RemoteException e) {e. printStackTrace () ;}@ Overrideprotected void onDestroy () {if (conn! = Null) {this. unbindService (conn);} super. onDestroy ();} class MyServiceConnection implements ServiceConnection {@ Overridepublic void onServiceConnected (ComponentName, IBinder service) {// get our self-written Binder object iService = IService. stub. asInterface (service) ;}@ Overridepublic void onServiceDisconnected (ComponentName name ){}}}


Okay, so far our code has been written. Now we can test it. The output of the System. out tag will be enough.

Here, let's talk about the application of AIDL.

The following is an example of hanging up a phone call. We know that there is no hanging up operation in the Android api.

What should we do if our application has such a demand?

In fact, it is also very easy, that is, to use aidl to hang up the phone, So let's write about it now.

First, copy the two aidl files to the src directory.


You will certainly ask, these aidl files are found there, so I can tell you that these are found in the Android source code, that is, under the base


You can download it and see if it does not. Click here.


Since we copied the aidl file, we can write code.

Com. xiaobin. endcall. MainActivity

Package com. xiaobin. endcall; import java. lang. reflect. method; import com. android. internal. telephony. ITelephony; import android. app. activity; import android. OS. bundle; import android. OS. IBinder; import android. view. view; public class MainActivity extends Activity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. setContentView (R. layout. main);} public void click (View v) {endCall ();} // call the private void endCall () {try {// obtain android through reflection. OS. the object of the getService Method in ServiceManager is method Method = Class. forName ("android. OS. serviceManager "). getMethod ("getService", String. class); // call the getService method through reflection, get the IBinder object, and then perform the aidl IBinder iBinder = (IBinder) method. invoke (null, new Object [] {TELEPHONY_SERVICE}); ITelephony telephony = ITelephony. stub. asInterface (iBinder); telephony. endCall ();} catch (Exception e) {e. printStackTrace ();}}}


Because the service is not public, we need to use reflection to call this service to obtain the Binder object.


Another important point is that you need to add the corresponding permissions.

    <uses-permission android:name="android.permission.CALL_PHONE"/>

Okay, now, the operation of hanging up the phone is complete. You can test it. Then, the blacklist of the project of our mobile guard also has the operation of hanging up the phone.


Download source code today

 

 

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.