Android Aidl Practice Pass Simple string

Source: Internet
Author: User

* The server and client of this demo are written in reverse.

New Project Client,server new Aidl file on server side, content:

Imyaidlinterface.aidl
Package com.example.server;

Declare any non-default types here with import statements

Interface Imyaidlinterface {

String getString ();
}

Make project automatically generates Java classes in Server\build\generated\source\aidl\debug\com\example\server

Create the same file in client and generate Java class

* Note that the two package names should be consistent

Writing service services in server

 PackageCom.example.client;

ImportAndroid.app.Service;
ImportAndroid.content.Intent;
ImportAndroid.os.IBinder;
ImportAndroid.os.RemoteException;
Importandroid.support.annotation.Nullable;
ImportAndroid.util.Log;

ImportCom.example.server.IMyAidlInterface;

/**
* Created by LUOZHENLONGHP on 2017/5/13.
*/

Public classMyServiceextendsService {
Public static FinalStringTAG="MyService";
@Nullable
@Override
PublicIBinder Onbind (Intent Intent) {
returnresult;
}

Imyaidlinterface.stubresult=NewImyaidlinterface.stub () {

@Override
PublicString getString ()throwsremoteexception {
return"You get It";
}
};

@Override
Public voidOnCreate () {
Log.I(TAG,"OnCreate is called");
Super. OnCreate ();
}

@Override
public intOnstartcommand (Intent Intent,intFlagsintStartid) {
Log.I(TAG,"Onstartcommand is called");
return Super. Onstartcommand (Intent, flags, Startid);
}
}

Registering in the Menifest file
<android: Name=
// This indicates the process name
Android :p rocess= "Com.test.myservice">

<intent-filter>
<android: name="Android.intent.action.RESPOND_VIA_MESSAGE"> </Action>
</Intent-filter>
</Service>

Write code binding service on client side
Bindmyservice ()
{
Intent ();
Here's the action.
Intent.setaction ("Android.intent.action.RESPOND_VIA_MESSAGE");
This is the service side of the package name, this time the demo write the reverse
Intent.setpackage ("Com.example.client");
Serviceconnection connection =NewServiceconnection () {
@Override
Public voidonserviceconnected (componentname name, IBinder service) {
Get an example here
Imyaidlinterface= Imyaidlinterface.stub.Asinterface(service);

Try{
String result =Imyaidlinterface. getString ();
Toast.Maketext(Mainactivity. This, result, Toast.Length_short). Show ();
}Catch(RemoteException e) {
E.printstacktrace ();
}


}

@Override
Public voidonservicedisconnected (componentname name) {

}
};

Bindservice (Intent,connection,bind_auto_create);
}

Done

Android Aidl Practice Pass Simple string

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.