欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

Android基本功:跨进程调用Services(AIDL Service)

来源: 开发者 投稿于  被查看 33402 次 评论:117

Android基本功:跨进程调用Services(AIDL Service)


package com.example.aidlservice; 
 
interface ICat { 
    String getColor(); 
    double getWeight(); 
} 

package com.example.aidlservice; 
import java.util.Timer; 
import java.util.TimerTask; 
import com.example.aidlservice.ICat.Stub; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.os.RemoteException; 
 
public class AidlService extends Service { 
    String[] colors = new String[] { "红色", "黄色", "黑色" }; 
    double[] weights = new double[] { 2.3, 3.1, 1.58 }; 
    private String color; 
    private double weight; 
    private CatBinder catBinder; 
    Timer timer = new Timer(); 
 
    @Override 
    public void onCreate() { 
        super.onCreate(); 
        catBinder = new CatBinder(); 
        timer.schedule(new TimerTask() { 
 
            @Override    
            public void run() { 
                // 随机地改变service组件内的color,weight属性的值 
                int rand = (int) (Math.random() * 3);   
                color = colors[rand]; 
                weight = weights[rand]; 
                System.out.println("---------" + rand); 
            } 
        }, 0, 800)}; 
 
    @Override 
    public IBinder onBind(Intent arg0) { 
        /**   
        * 返回CatBinder对象,在绑定本地Service情况下, 
        * 该catBinder会直接传给客户端的ServiceConnected对象的ServiceConnected 
        * ()方法的第二个参数;在绑定远程Service的情况下  
        * ,只将catBinder对象的代理传给客户端的ServiceConnected对象的ServiceConnected()方法的第二个参数 
        */ 
        return catBinder; 
    } 
 
    @Override 
    public void onDestroy() { 
        timer.cancel(); 
    } 
 
    /**    
    * 继承Stub,也就是实现了ICat接口,并实现了IBinder接口 
    *  
    * @author pengcx 
    * 
    */ 
    public class CatBinder extends Stub { 
        @Override 
        public String getColor() throws RemoteException { 
            return color; 
        } 
 
    @Override 
        public double getWeight() throws RemoteException { 
            return weight; 
        } 
    } 
} 

<service android:name="com.example.aidlservice.AidlService" > 
    <intent-filter> 
        <action android:name="org.crazyit.aidl.action.AIDL_SERVICE" /> 
    </intent-filter> 
 </service> 

package com.example.aidlclient; 
 
import com.example.aidlservice.ICat; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.os.RemoteException; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.app.Activity; 
import android.app.Service; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.content.ServiceConnection; 
 
public class AidlClient extends Activity { 
    private ICat catService; 
    private Button getButton; 
    private EditText colorEditText, weightEditText; 
 
    private ServiceConnection conn = new ServiceConnection() { 
        @Override 
        public void onServiceDisconnected(ComponentName name) { 
            catService = null; 
        } 
 
        @Override 
        public void onServiceConnected(ComponentName name, IBinder service) { 
            // 获取远程Service的onBinder方法返回的对象代理 
            catService = ICat.Stub.asInterface(service); 
        } 
    }; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_aidl_client); 
 
        getButton = (Button) findViewById(R.id.getbutton); 
        colorEditText = (EditText) findViewById(R.id.coloredittext); 
        weightEditText = (EditText) findViewById(R.id.weightedittext); 
 
        // 创建所需要绑定的Service的Intent 
        Intent intent = new Intent(); 
        intent.setAction("org.crazyit.aidl.action.AIDL_SERVICE"); 
        // 绑定远程的服务 
        bindService(intent, conn, Service.BIND_AUTO_CREATE); 
             
        getButton.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
    // 获取并显示远程service的状态 
    try { 
        colorEditText.setText(catService.getColor()); 
        weightEditText.setText(catService.getWeight() + ""); 
    } catch (RemoteException e) { 
        e.printStackTrace(); 
    } 
} 
       }); 
    } 
 
    @Override 
    protected void onDestroy() { 
        super.onDestroy(); 
        // 解除绑定 
        this.unbindService(conn); 
    } 
} 

相关频道:

用户评论