绑定服务促进进程间的通信
又称远程服务。
例如运用其他应用的登录等。
下面是一个小例子:
一、
1、实例化seiviceConnection,实例化intent,自定义一个sevice类。在清单文件中配置service;
<service android:name=".Myservice"
android:exported="true"
></service>
</application>
2、在Activity的onResume()中绑定服务。用bindService(intent,seiviceConnection,Service. BIND_AUTO_CREAT);
。要将自定义service类中的onBind()的返回值null变为IBinder,所以自己写个类继承Bind(相当于dao),然后将null改成返回自定义的Binder。
public class MainActivity extends AppCompatActivity {
private EditText name;
private EditText pwd;
private Intent intent;
private IMyAidlInterface iMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.et_main_01);
pwd = (EditText) findViewById(R.id.et_main_02);
intent = new Intent(this,Myservice.class);
} ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test","绑定成功");
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onResume() {
super.onResume();
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
public void post(View view){
Boolean flag= null;
try {
flag = iMyAidlInterface.Login(name.getText().toString(),pwd.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
Toast.makeText(this,""+flag,Toast.LENGTH_SHORT).show();
}
}
3、在包下面右击新建一个AIDL(Android 接口自定义语音)里面写要操作的方法,然后重新编译一下,自定义的Binder继承新建的AIDL.stub,重写方法,写你需要执行的操作,在onServiceConnected()中用新建的AIDL.stub.asInterface(iBinder),用拿到的新建的AIDL调用写的操作方法。
interface IMyAidlInterface {
boolean Login(String name,String pwd);
}
public class Myservice extends Service {
class mybind extends IMyAidlInterface.Stub {
@Override
public boolean Login(String name, String pwd) {
Log.i("test", "iii" + pwd);
if ("123".equals(pwd)&&"456".equals(name)){
return true;
}
return false;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new mybind();
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
}
二、
1、接下来建立另一个Module,绑定服务时的Intent,androi5.0以后,启动其他应用程序的服务,不允许用隐示。所以我们用显示的ComponentName,实例化ComponentName,两个参数(启动的应用程序的包名 你要启动的service类的包名.类名),然后intent.setComponent(componentName);同样是在onResume()中绑定服务。
public class MainActivity extends AppCompatActivity {
private EditText pwd;
private EditText name;
private Intent intent;
private IMyAidlInterface iMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.et_main_01);
pwd = (EditText) findViewById(R.id.et_main_02);
intent = new Intent();
//启动的应用程序的包名 你要启动的service类的包名.类名
ComponentName componentName=new ComponentName("com.example.myapplication","com.example.myapplication.Myservice");
intent.setComponent(componentName);
}
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("testaa", "iii");
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onResume() {
super.onResume();
bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
}
public void post(View view){
Boolean flag= null;
try {
Log.i("testaa", "iii"+name.getText().toString());
flag = iMyAidlInterface.Login(name.getText().toString(),pwd.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
Toast.makeText(this,""+flag,Toast.LENGTH_SHORT).show();
}
}
2、在project中找到你要用的aidl的项目,build-generated-source-aidl-debug下的包,将该包及里面的所要的文件拷到本项目的src-main-java中,(考好后包名要和之前保持一致)。在onServiceConnected()中用AIDL.stub.asInterface(iBinder),用拿到的新建的AIDL调用写的操作方法。
public class Myservice extends Service {
class mybind extends IMyAidlInterface.Stub {
@Override
public boolean Login(String name, String pwd) {
Log.i("test", "iii" + pwd);
if ("123".equals(pwd)&&"456".equals(name)){
return true;
}
return false;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new mybind();
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
}
<service android:name=".Myservice"
android:exported="true"
></service>
</application>
注意:在清单文件中配置service。保持另一个程序的服务处于开启运行状态。
如果对aidl感兴趣可了解它如何返回对象等其它知识。
例如: android之Service介绍之三 AIDL与传递对象
http://blog.csdn.net/chenzheng_java/article/details/6260238