Video Address: http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=877122&courseId= 712011
First, the question:
1, how to do an app different process communication?
2. Multiple app communication (different apps)
3. Inject event run script and invoke hidden API
Second app different process communication:
Knowledge Points:
1, Intent, binder
2. Service, Activity
3, Handler, view
4. Messenger, Message, Serviceconnection, IPC (Internal process Communication)
5. Bundle
6. Remote
7. PID
Then the following is a description of the implementation of the Principles and procedures:
1. Define a class that inherits from the service, use Onbind's life cycle, define a Messenger object on the server, and then return the object's Getbinder () through the Onbind () method to the client, which is used when binding
2, need to pass a handler object to Messenger, need to implement a subclass inherited from the handler class, and then the Replication Handlemessage () method, which is used to handle the message from the client, and according to the client's content, Make the appropriate action
3. Then register the service class defined above in Androidmanifest.xml, add android:process= ": Remote"
4, then define a client program, such as an activity class, increase the binding service and bind the service button, and then increase the response time, mainly called the Bindservice () method and the Unbindservice () method, Then in the bindservice need to use a Serviceconnection object, need to define this object in the process, the implementation of two methods, respectively, onserviceconnected () and onservicedisconnected ( ), and then get the service side of the IBinder object, to get the service side of the messenger, and then through this Messenger, to the service side of the message, the server can receive MSG, then processing the message, is the 2nd step of processing
5, the above to achieve from the client to the service side of the one-way communication, how to achieve two-way communication? is to write a handler class on the client side, and then initialize a client Messenger object with the object of the handler class, then assign the local Messenger to Msg.replyto and then the server Messenger object. Send (MSG) You can send the message out, and then the server can get a messenger from the client that can communicate with each other between client and service.
Then on the program code:
Service programs in the Com.example.twomessengerservice.service
PackageCom.example.twomessengerservice.service;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Handler;ImportAndroid.os.IBinder;ImportAndroid.os.Message;ImportAndroid.os.Messenger;Importandroid.os.RemoteException;ImportAndroid.widget.Toast; Public classMessengerserviceextendsService { Public Static Final intSay_hello = 0x1; PrivateMessenger Mymessenger =NewMessenger (NewIncominghandler ()); classIncominghandlerextendshandler{@Override Public voidhandlemessage (Message msg) {//TODO auto-generated Method Stub Switch(msg.what) { CaseSAY_HELLO:Toast.makeText (Getapplicationcontext (),"Processing of service", Toast.length_short). Show (); //when a value needs to be returned, it is transmitted by means of a messageMessage newmsg =Message.obtain (); Newmsg.what=Say_hello; //get the messenger from Msg.reply to the clientMessenger Cmessenger =Msg.replyto; Try{cmessenger.send (newmsg); }Catch(RemoteException e) {e.printstacktrace (); } Break; default: Break; } Super. Handlemessage (msg); }} @Override Publicibinder onbind (Intent Intent) {//TODO auto-generated Method Stub returnMymessenger.getbinder (); }}
Client programs in Com.example.twomessengerservice:
PackageCom.example.twomessengerservice;ImportCom.example.twomessengerservice.service.MessengerService;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.IBinder;ImportAndroid.os.Message;ImportAndroid.os.Messenger;Importandroid.os.RemoteException;Importandroid.app.Activity;ImportAndroid.app.Service;ImportAndroid.content.ComponentName;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateButton Buttonbind; PrivateButton Buttonunbind; BooleanIsbound =false; Messenger Rmessenger=NULL; Messenger Mmessenger=NULL; Private FinalString tag = "Activity"; classMhandlerextendshandler{@Override Public voidhandlemessage (Message msg) {//TODO auto-generated Method Stub Switch(msg.what) { CaseMessengerService.SAY_HELLO:Toast.makeText (mainactivity. This, "Toast of the client", Toast.length_short). Show (); Break; default: Break; }}} serviceconnection conn=Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {//TODO auto-generated Method StubRmessenger =NULL; } @Override Public voidonserviceconnected (componentname name, IBinder service) {//TODO auto-generated Method StubRmessenger =NewMessenger (service);//get the Messenger to the serviceMmessenger =NewMessenger (NewMhandler ());//Local MessengerSendMessage (); } }; protected voidSendMessage () {//TODO auto-generated Method StubMessage msg = Message.obtain (NULL, Messengerservice.say_hello); Msg.replyto =Mmessenger; Try{rmessenger.send (msg); } Catch(RemoteException e) {e.printstacktrace (); }} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Buttonbind=(Button) Findviewbyid (R.id.buttonbind); Buttonunbind=(Button) Findviewbyid (R.id.buttonunbind); Buttonbind.setonclicklistener (l); Buttonunbind.setonclicklistener (l); } view.onclicklistener L=NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub Switch(V.getid ()) { CaseR.id.buttonbind:toast.maketext (Getapplicationcontext (),"Binding Service", Toast.length_short). Show (); LOG.V (Tag,"Buttonbind"); Isbound= Bindservice (NewIntent (mainactivity. This, Messengerservice.class), Conn, service.bind_auto_create); Toast.maketext (Getapplicationcontext (),"Receive messages from server side", Toast.length_short). Show (); Break; CaseR.id.buttonunbind:if(Isbound =true) {LOG.V (tag,"Buttonunbind"); Toast.maketext (Getapplicationcontext (),"Unbind", Toast.length_short). Show (); Unbindservice (conn); } Break; default: Break; } } }; @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
Androidmanifest.xml configuration file:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.twomessengerservice"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "8"android:targetsdkversion= "/>" <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <Activity Android:name= "Com.example.twomessengerservice.MainActivity"Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter& Gt </activity> <service android:name = ". Service. Messengerservice "
android:process= ": Remote" ></service> </application></manifest>
Cross-process (communication between different processes in the same app)--android Automated test learning process