Implement IPC communication on the android Upper Layer

Source: Internet
Author: User

The android upper layer implements the IPC Communication Server, and directly goes to the Code:

Package COM. KZ. server; import android. app. service; import android. content. intent; import android. OS. bundle; import android. OS. handler; import android. OS. ibinder; import android. OS. message; import android. OS. messenger; import android. OS. remoteException; import android. util. log; public class Server extends Service {Private Static final string tag = "server"; Private Static final int bind = 1100; Private Static final int unbind = 1200; class incominghandler extends handler {@ overridepublic void handlemessage (Message MSG) {log. I (TAG, "---------- handlemessage ()"); Switch (MSG. what) {Case BIND: Message bindmessage = message. obtain (null, bind); try {MSG. replyto. send (bindmessage);} catch (RemoteException e) {e. printstacktrace ();} break; Case unbind: Message unbindmessage = message. obtain (null, unbind); try {MSG. replyto. send (unbindmessage);} catch (RemoteException e) {e. printstacktrace ();} break; Case commandlist. subtitle: String [] resultsubtitles = getsubtitles (); message subtitlemessage = message. obtain (null, commandlist. subtitle); bundle subtitlebundle = new bundle (); subtitlebundle. putstringarray ("resultsubtitles", resultsubtitles); subtitlemessage. setdata (subtitlebundle); try {MSG. replyto. send (subtitlemessage);} catch (RemoteException e) {e. printstacktrace ();} break; Case commandlist. time_current: integer resulttimecurrent = gettimecurrent (); message timecurrentmessage = message. obtain (null, commandlist. time_current); bundle timecurrentbuddle = new bundle (); timecurrentbuddle. putint ("resulttimecurrent", resulttimecurrent); timecurrentmessage. setdata (timecurrentbuddle); try {MSG. replyto. send (timecurrentmessage);} catch (RemoteException e) {e. printstacktrace ();} break; Case commandlist. time_total: integer resulttimetotal = gettimetotal (); message timetotalmessage = message. obtain (null, commandlist. time_total); bundle timetotalbuddle = new bundle (); timetotalbuddle. putint ("resulttimetotal", resulttimetotal); timetotalmessage. setdata (timetotalbuddle); try {MSG. replyto. send (timetotalmessage);} catch (RemoteException e) {e. printstacktrace ();} break; Case commandlist. language: String [] resultlanguages = getages (); message receivagemessage = message. obtain (null, commandlist. language); bundle implements agebundle = new bundle (); implements agebundle. putstringarray ("resultlanguages", resultlanguages); extends agemessage. setdata (receivagebundle); try {MSG. replyto. send (receivagemessage);} catch (RemoteException e) {e. printstacktrace ();} break; default: Super. handlemessage (MSG) ;}} messenger mmessenger = new messenger (New incominghandler (); @ overridepublic void oncreate () {log. I (TAG, "---------- oncreate ()"); super. oncreate () ;}@ overridepublic ibinder onbind (intent) {log. I (TAG, "---------- onbind ()"); Return mmessenger. getbinder () ;}@ overridepublic void ondestroy () {log. I (TAG, "---------- ondestroy ()"); super. ondestroy ();} private string [] getsubtitles () {log. I (TAG, "---------- getsubtitles ()"); return New String [] {"Chinese subtitles", "English Subtitles", "Thai Subtitles "};} private string [] getages () {log. I (TAG, "---------- getages ()"); return New String [] {"Chinese", "English", "Thai" };} private integer gettimetotal () {log. I (TAG, "---------- gettimetotal ()"); Return 150000;} private integer gettimecurrent () {log. I (TAG, "---------- gettimecurrent ()"); Return 132000 ;}}

The code for implementing the IPC communication server on the android upper layer:

Package COM. KZ. client; import android. app. activity; import android. content. componentname; import android. content. intent; import android. content. serviceconnection; import android. OS. bundle; import android. OS. handler; import android. OS. ibinder; import android. OS. message; import android. OS. messenger; import android. OS. remoteException; import android. view. view; import android. widget. button; import android. widget. li Nearlayout; import android. widget. textview; public class clientactivity extends activity implements changeinfocallback {private Boolean isbind = false; private textview txt, txtsubtitle, txtlanguage; private linearlayout; private button bind, unbind, subtitle, language; private managechangeinfo manageinfo; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); Setcontentview (R. layout. main); manageinfo = new managechangeinfo (); manageinfo. registercallback (this); TXT = (textview) callback); txtsubtitle = (textview) Publish subtitle); txtlanguage = (textview) findviewbyid(r.id.txt language); Bind = (button) findviewbyid (R. id. bindbtn); unbind = (button) findviewbyid (R. id. unbindbtn); subtitle = (button) findviewbyid (R. id. subtitle); Language = (Button) findviewbyid (R. id. language); layout = (linearlayout) findviewbyid (R. id. layout); bind. setonclicklistener (click); unbind. setonclicklistener (click); subtitle. setonclicklistener (click); language. setonclicklistener (click); layout. setonclicklistener (click) ;}@ overrideprotected void onpause () {super. onpause (); If (isbind) {unbindservice (conn); isbind = false ;}} private view. onclicklistener click = new v Iew. onclicklistener () {@ overridepublic void onclick (view v) {int id = v. GETID (); Switch (ID) {case R. id. bindbtn: intent service = new intent ("com. KZ. DVD "); bindservice (Service, Conn, bind_auto_create); break; case R. id. unbindbtn: If (isbind & mservice! = NULL) {unbindservice (conn); message MSG = message. obtain (null, unbind); MSG. replyto = mmessenger; try {mservice. send (MSG);} catch (RemoteException e) {e. printstacktrace ();} isbind = false;} break; case R. id. subtitle: sendmessage (commandlist. subtitle); break; case R. id. language: sendmessage (commandlist. language); break ;}}; private void sendmessage (INT action) {If (isbind & mservice! = NULL) {message MSG = message. obtain (null, action); MSG. replyto = mmessenger; try {mservice. send (MSG);} catch (RemoteException e) {e. printstacktrace () ;}} Private Static final int bind = 1100; Private Static final int unbind = 1200; private class incominghandler extends handler {@ overridepublic void handlemessage (Message MSG) {Switch (MSG. what) {Case BIND: layout. setvisibility (view. visible); TXT. settext ("bind"); break; Case unbind: layout. setvisibility (view. invisible); TXT. settext ("Unbind"); txtsubtitle. settext ("Subtitle -->"); txtlanguage. settext ("Language -->"); break; Case commandlist. subtitle: String [] resultsubtitles = MSG. getdata (). getstringarray ("resultsubtitles"); manageinfo. onchangesubtitle (getstringfromarray (resultsubtitles); break; Case commandlist. language: String [] resultlanguages = MSG. getdata (). getstringarray ("resultlanguages"); manageinfo. onchangelanguage (getstringfromarray (resultlanguages); break; default: Super. handlemessage (MSG) ;}}; private messenger mmessenger = new messenger (New incominghandler (); Private messenger mservice; private serviceconnection conn = new serviceconnection () {@ overridepublic void onserviceconnected (componentname, ibinder Service) {isbind = true; mservice = new messenger (service); message MSG = message. obtain (null, bind); MSG. replyto = mmessenger; try {mservice. send (MSG);} catch (RemoteException e) {e. printstacktrace () ;}@ overridepublic void onservicedisconnected (componentname name) {mservice = NULL ;};@ overridepublic void changesubtitle (string subtitle) {txtsubtitle. settext ("Subtitle -->" + subtitle) ;}@ overridepublic void changelanguage (string language) {txtlanguage. settext ("Language -->" + language);} private string getstringfromarray (string [] STRs) {stringbuilder sb = new stringbuilder (); sb. append ('['); For (string STR: STRs) {sb. append (STR ). append (",");} sb. deletecharat (sb. length ()-1); sb. append (']'); return sb. tostring ();}}

The main process is the above two categories, some interfaces are not published, and reference materials are the topics of services on the official website.

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.