慕姐1217228
2018-02-28 12:01
空指针的问题咋解决,我换了action方式也不行.startService取不到服务器里面的Service类 startService(new Intent(this,Class.forName("aidl.msd.com.aidldemo.AidlService")));像这样也不行
另外服务端要注册服务,并且允许远程,不然会发生安全性异常
<service android:name=".MService"
android:process=":remote"
android:exported="true"
/>
package com.jin.myapplication;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.jin.appspare.AidlObj;
import com.jin.appspare.IMyAidlInterface;
import com.jin.appspare.Person;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText mEt1;
private EditText mEt2;
private TextView mTv;
private Button mBt;
private IMyAidlInterface iMyAidlInterface;
private AidlObj aidlObj;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("client", "服务已经连接" + name);
//拿到远程的服务-----------------------------------------------
// iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
aidlObj = AidlObj.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("client", "服务已经断开" + name);
aidlObj = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bindService();
initEvent();
}
private void initView() {
mEt1 = (EditText) findViewById(R.id.editText);
mEt2 = (EditText) findViewById(R.id.editText2);
mTv = (TextView) findViewById(R.id.textView);
mBt = (Button) findViewById(R.id.button);
}
private void initEvent() {
mBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// try {
// int number1 = Integer.parseInt(mEt1.getText().toString());
// int number2 = Integer.parseInt(mEt2.getText().toString());
// int add = iMyAidlInterface.add(number1, number2);
// mTv.setText(add + "");
// }catch (Exception e) {
// e.printStackTrace();
// }
try {
//---------------------------------------------------------------------
List<Person> abc = aidlObj.add(new Person("ABC", 20));
for (Person person : abc) {
Log.e("person-name", person.getName());
Log.e("person-age", person.getAge() + "");
}
//---------------------------------------------------------------------
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
private void bindService() {
//获取服务端
Intent intent = new Intent();
//显示Intent启动绑定服务
intent.setComponent(new ComponentName("com.jin.appspare","com.jin.appspare.MService"));
//一绑定,就会启动
Log.e("client","开始绑定服务端");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}
AIDL-小白成长记
42599 学习 · 57 问题
相似问题