设为首页 加入收藏

TOP

Android学习笔记--远程服务的使用(二)
2017-10-12 10:07:48 】 浏览:10119
Tags:Android 学习 笔记 远程 服务 使用
。其含义请自行google。

OK,到此为止,服务端的工作已经全部结束了。接下来我们说下客户端如何使用服务端提供的接口

3、客户端的实现

1、在Eclipse中新建工程,名称为Client;然后把Server工程中的com.example.aidl包整个拷贝到Client工程的src目录下;工程结构如下所示

image

2、定义布局文件,在布局文件中增加两个按钮,一个是bind服务,一个是unbind服务。activity_main.xml如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/binderButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="bind" />
    
    
    <Button
        android:id="@+id/unbinderButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="unbind" />

</LinearLayout>

3、在Acitivity中使用远程服务,MainActivity.java内容如下

package com.example.client;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.aidl.IRemoteService;
 
public class MainActivity extends Activity {
    
    private String TAG = "com.example.aidl";
    private IRemoteService remoteService;
    private Button mBindButton;
    private Button mUnbindButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBindButton = (Button)findViewById(R.id.binderButton);
        mBindButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d(TAG, "Bind Service");
                //Intent intent = new Intent(IRemoteService.class.getName());
                /*新建Intent,指定Service的AndroidMainfest.xml中声明的Action名称*/
                Intent intent = new Intent("com.example.aidl");
                /*在Android5.0以后,不允许隐式调用服务,故再次指定服务所在包名*/
                intent.setPackage("com.example.service");
                /*绑定服务*/
                bindService(intent, conn, Context.BIND_AUTO_CREATE);
            }
        });
        
        mUnbindButton = (Button)findViewById(R.id.unbinderButton);
        mUnbindButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d(TAG, "UnBind Service");
                /*解绑服务*/
                unbindService(conn);
            }
        });
    }
     
    ServiceConnection conn = new ServiceConnection() {
         
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "UnBind Service success!");
        }
         
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "Bind Service success!");
            /*获取服务端handle*/
            remoteService = IRemoteService.Stub.asInterface(service);
            try {
                int pid1 = remoteService.getPid();
                int pid2 = remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我们的爱,我明白");
                /*打印带有getPid接口和basicTypes接口时服务端的线程号*/
                Log.d(TAG, &qu
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇KOTLIN开发语言文档(官方文档) .. 下一篇OpenGL ES学习笔记(二)——平滑..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目