调用远程service

Posted znsongshu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用远程service相关的知识,希望对你有一定的参考价值。

Service端

  <service android:name="com.atguigu.l07_service.remote.MyRemoteService">
            <intent-filter>
                <action android:name="com.atguigu.l07_service.remote.MyRemoteService.Action"/>
            </intent-filter>
        </service>
public class MyRemoteService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("TAG", "onBind()");
        return new StudentService();
    }
    
    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG", "onUnbind()");
        return super.onUnbind(intent);
    }
    
    //处理Student相关的业务逻辑类
    class StudentService extends IStudentService.Stub {
        @Override
        public Student getStudentById(int id) throws RemoteException {
            Log.e("TAG", "Service getStudentById() "+id);
            return new Student(id, "Tom", 10000);
        }
    }

}

client

public class MainActivity extends Activity {

    private EditText et_aidl_id;

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

        et_aidl_id = (EditText) findViewById(R.id.et_aidl_id);
    }

    private ServiceConnection conn;
    private IStudentService studentService;

    public void bindRemoteService(View v) {

        Intent intent = new Intent(
                "com.atguigu.l07_service.remote.MyRemoteService.Action");
        if (conn == null) {
            conn = new ServiceConnection() {

                @Override
                public void onServiceDisconnected(ComponentName name) {

                }

                @Override
                public void onServiceConnected(ComponentName name,
                        IBinder service) {
                    Log.e("TAG", "onServiceConnected()");
                    studentService = IStudentService.Stub.asInterface(service);
                }
            };
            bindService(intent, conn, Context.BIND_AUTO_CREATE);
            Toast.makeText(this, "绑定Service", 0).show();
        } else {
            Toast.makeText(this, "已经绑定Service", 0).show();
        }

    }

    public void invokeRemote(View v) throws RemoteException {
        if(studentService!=null) {
            int id = Integer.parseInt(et_aidl_id.getText().toString());
            Student student = studentService.getStudentById(id);
            Toast.makeText(this, student.toString(), 0).show();
        }
    }

    public void unbindRemoteService(View v) {
        if (conn != null) {
            unbindService(conn);
            conn = null;
            studentService = null;
            Toast.makeText(this, "解绑Service", 0).show();
        } else {
            Toast.makeText(this, "还未绑定Service", 0).show();
        }
    }
}

 

以上是关于调用远程service的主要内容,如果未能解决你的问题,请参考以下文章

如何调用web service接口的某个方法

普通方法实现——远程方法调用RMI代码演示

从远程位置访问 Service Fabric RPC 接口

一个简单的AXIS远程调用Web Service示例

Service Fabric 对无状态服务的远程调用未返回、卡住

Android调用远程Service的参数和返回值都需要实现Parcelable接口