- 最好用两个app来练习,一个提供service,一个调用。
服务端提供service的代码
1.创建aidl文件
在android studio 的项目中右键new file,选择aidl文件夹,然后系统会自动创建相应的 aidl文件。
如图:
image.jpeg
2.定义所需要的方法,同步下工程
在aidl文件中定义方法
// IMyAidlInterface.aidl
package com.example.zyl.learnaudio;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
boolean insertData2Db(int clickId);
}
sync工程之后会在build目录下生成对应的aidl文件(里面是aidl的sdk生成的代码),如图:
image.jpeg
3.编写服务端提供的service
package com.example.zyl.learnaudio;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
public class MyService extends Service {
private boolean serviceRunning;
//用于创建Service的方法,只能调用一次
@Override
public void onCreate() {
super.onCreate();
System.out.println("--onCreate--");
serviceRunning = true;
//方便看服务是否活着
new Thread() {
@Override
public void run() {
while (serviceRunning) {
System.out.println("--Service运行中--");
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("zyl_aidl", "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("zyl_aidl", "onUnbind: ");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
serviceRunning = false;
Log.d("zyl_aidl", "onDestroy: ");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("zyl_aidl", "onBind: ");
return new MyBinder();
}
private class MyBinder extends IMyAidlInterface.Stub {
@Override
public boolean insertData2Db(int clickId) throws RemoteException {
Log.d("zylaidl", "clickId = " + clickId);
if (clickId > 0) {
return true;
}
return false;
}
}
}
androidManifest中注册服该服务
<!--注册服务,其中属性要加上export:true 让外部程序可以访问。-->
<service
android:name=".MyService"
android:exported="true">
<intent-filter>
<action android:name="com.zyl.aidl"/>
</intent-filter>
</service>
以上服务端的搞定了。
客户端实现使用
1.把服务端的aidl文件夹下全部拷贝到这个项目中,然后sync一下。这样才能引用到相应的类和方法。
2.绑定服务,实现调用。
package com.example.zyl;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.zyl.clent.R;
import com.example.zyl.learnaudio.IMyAidlInterface;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private IMyAidlInterface mIMyAidlInterface;
private final String TAG = "zyl_aidl";
private boolean isBind;
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
if (mIMyAidlInterface == null) {
Toast.makeText(this, "mIMyAidlInterface == null", Toast.LENGTH_SHORT).show();
} else {
try {
boolean success = mIMyAidlInterface.insertData2Db(222);
Toast.makeText(this, "success = " + success, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
public void bind(View view) {
mIntent = new Intent();
mIntent.setAction("com.zyl.aidl");
mIntent = new Intent(createExplicitFromImplicitIntent(this, mIntent));
isBind = bindService(mIntent, serviceConnection, BIND_AUTO_CREATE);
Log.d(TAG, "isBind: " + isBind);
}
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
public void unbind(View view) {
if (isBind){
unbindService(serviceConnection);
mIMyAidlInterface = null;
isBind = false;
}
}
}
网友评论