码迷,mamicode.com
首页 > 其他好文 > 详细

调用远程service

时间:2018-07-23 18:06:26      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:this   str   div   _id   tom   remote   string   ibinder   prot   

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

标签:this   str   div   _id   tom   remote   string   ibinder   prot   

原文地址:https://www.cnblogs.com/znsongshu/p/9355893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!