Android IPC通信bindService不能回调onServiceConnected方法

SugarAnnie 2016-10-16 12:58:00
Server端:
用aidl定义了一个接口,接口中有个add方法,然后写了一个Service实现了这个接口

public class CalculateService extends Service {
private static final String TAG = "CalculateService";

@Override
public void onCreate() {
super.onCreate();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.d(TAG, "onBind()");
return mBinder;
}

private final ICalculateInterface.Stub mBinder = new ICalculateInterface.Stub() {

@Override
public int add(int a, int b) throws RemoteException {
Log.d(TAG, "远程计算中...");
Log.d(TAG, a + " + " + b + " = " + (a + b));

return a + b;
}

@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}
};
}

在AndroidManifest.xml中注册了服务

<service
android:name=".CalculateService">
<intent-filter>
<action android:name="CalculateService"/>
</intent-filter>
</service>

Client端

public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private EditText num1 = null;
private EditText num2 = null;
private TextView result = null;
private Button btn = null;
private ICalculateInterface calculateInterface = null;

ServiceConnection serviceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
calculateInterface = ICalculateInterface.Stub.asInterface(service);
if (calculateInterface == null) {
Log.d(TAG, "calculateInterface 对象为空");
} else {
Log.d(TAG, "获取 calculateInterface 对象");
}
}

@Override
public void onServiceDisconnected(ComponentName name) {
calculateInterface = null;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
result = (TextView) findViewById(R.id.tv4);
btn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(new ButtonClickListener());
}

class ButtonClickListener implements View.OnClickListener {

@Override
public void onClick(View v) {
Intent intent = new Intent("CalculateService");
Intent eintent = new Intent(createExplicitFromImplicitIntent(MainActivity.this, intent));
bindService(eintent, serviceConnection, Context.BIND_AUTO_CREATE);
if (calculateInterface == null)
Log.d(TAG, "未连接服务器");
try {
int res = calculateInterface.add(Integer.parseInt(num1.getText().toString()), Integer.parseInt(num2.getText().toString()));
result.setText(res + "");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

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;
}
}

运行的时候int res = calculateInterface.add那行报错:
java.lang.NullPointerException: Attempt to invoke interface method 'int com.cumt.aidl.ICalculateInterface.add(int, int)' on a null object reference
打印“未连接服务器”,onServiceConnected中的代码也没有执行
求大神帮忙看看我哪里学错了,或者少些了什么??
...全文
488 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
focuseyes 2016-10-24
  • 打赏
  • 举报
回复
bindService() 方法不是你定义的内部类的方法,
SugarAnnie 2016-10-16
  • 打赏
  • 举报
回复
但是不知道原因是什么,求大神赐教!!
SugarAnnie 2016-10-16
  • 打赏
  • 举报
回复
找到原因了,把下面这几行从onClick()方法中移到onCreate方法中就可以了

Intent intent = new Intent();
intent.setAction("CalculateService");
intent.setPackage("com.cumt.aidldemoserver");
 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧