android interface define language
跨进程通信前提:2个进程均已启动
1,跨进程启动Service
Intent intent = new Intent(); //OTHER_SERVICE 另一进程Service包名+ClassNameintent.setComponent(new ComponentName(OTHER_PACKAGE, OTHER_SERVICE)); startService(intent);
2,跨进程绑定Service
2.1 Service进程创建aidl
2.2 onBind返回绑定对象
@Override public IBinder onBind(Intent intent) { return new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } }; }
2.3 在第1个进程调用bindService与unbindService
package com.example.superleeq.myapplication; public class MainActivity extends Activity implements ServiceConnection { final static String OTHER_PACKAGE = "com.example.superleeq.myapplication2"; final static String OTHER_SERVICE = "com.example.superleeq.myapplication2.MyService"; boolean isbinded; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { findViewById(R.id.activity_main_button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setComponent(new ComponentName(OTHER_PACKAGE, OTHER_SERVICE)); isbinded = bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE); } }); findViewById(R.id.activity_main_button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isbinded) { unbindService(MainActivity.this); isbinded = false; } else { LogUtil.log("already unbindservice"); } } }); } @Override public void onServiceConnected(ComponentName name, IBinder service) { LogUtil.log("onServiceConnected ComponentName name = " + name + " , IBinder service = " + service); } @Override public void onServiceDisconnected(ComponentName name) { LogUtil.log("onServiceDisconnected ComponentName name = " + name); } }
3,跨进程与Service通信
3.1 aidl folder + package + class必须两个工程完全一致
3.2 app2 Service
package com.example.superleeq.myapplication2; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MyService extends Service { private String mData = "default data"; private boolean running; public MyService() { } @Override public void onCreate() { super.onCreate(); Log.e("lq", "MyService.onCreate"); new Thread(new Runnable() { @Override public void run() { running = true; while (running){ Log.e("lq", "MyService.mdata=" + mData); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } @Override public void onDestroy() { super.onDestroy(); Log.e("lq", "MyService.onDestroy"); running = false; } @Override public IBinder onBind(Intent intent) { Log.e("lq", "MyService.onBind Intent intent=" + intent); return new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public void setData(String data) throws RemoteException { mData = data; } }; } }
3.3 app1 mainActivity
package com.example.superleeq.myapplication; 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.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.EditText; import com.example.superleeq.myapplication.util.LogUtil; import com.example.superleeq.myapplication2.IMyAidlInterface; public class MainActivity extends Activity implements ServiceConnection { final static String OTHER_PACKAGE = "com.example.superleeq.myapplication2"; final static String OTHER_SERVICE = "com.example.superleeq.myapplication2.MyService"; boolean isbinded; IMyAidlInterface binder = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { findViewById(R.id.activity_main_button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //android5.0以后仅支持显示意图启动 intent.setComponent(new ComponentName(OTHER_PACKAGE, OTHER_SERVICE)); isbinded = bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE); } }); findViewById(R.id.activity_main_button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { binder = null; if (isbinded) { unbindService(MainActivity.this); isbinded = false; } else { LogUtil.log("already unbindservice"); } } }); final EditText editText = (EditText) findViewById(R.id.mInput); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (binder != null) { try { binder.setData(s.toString()); } catch (RemoteException e) { e.printStackTrace(); } } } }); findViewById(R.id.setdata).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (binder != null) { try { binder.setData(editText.getText().toString()); } catch (RemoteException e) { e.printStackTrace(); } } } }); } @Override public void onServiceConnected(ComponentName name, IBinder service) { LogUtil.log("onServiceConnected ComponentName name = " + name + " , IBinder service = " + service); //不能强制转换,否则报错 binder = IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { //onServiceDisconnected()方法在正常情况下即使unbindService是不被调用的,它的调用时机是当Service服务被异外销毁时,例如内存的资源不足时 LogUtil.log("onServiceDisconnected ComponentName name = " + name); } }
时间: 2024-10-06 09:05:50