首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

AIDL代码示范

2012-08-24 
AIDL代码示例1. ITaskCallback.aidlpackage com.cmcc.demointerface ITaskCallback {void actionPerforme

AIDL代码示例
1. ITaskCallback.aidl    

package com.cmcc.demo;    

interface ITaskCallback {    

    void actionPerformed(int actionId);    

}  
 

2. ITaskBinder.aidl    

package com.cmcc.demo;  

import com.cmcc.demo.ITaskCallback;  

interface ITaskBinder {    

    boolean isTaskRunning();    

    void stopRunningTask();  

    void registerCallback(ITaskCallback cb);

    void unregisterCallback(ITaskCallback cb);  
 
}  
 

3.  MyService.java  
 

package com.cmcc.demo;   

import android.app.Service;    

import android.content.Intent;    

import android.os.IBinder;    

import android.os.RemoteCallbackList;   

import android.os.RemoteException;   

import android.util.Log;    

public class MyService extends Service {  
    @Override 

    public void onCreate() {    

        printf("service create");   

    }  
 

    @Override  

    public void onStart(Intent intent, int startId) {  

        printf("service start id=" + startId);   

        callback(startId);  

    }     

 

    @Override  

    public IBinder onBind(Intent t) {   

        printf("service on bind");    

        return mBinder;   

    }    

    @Override  

    public void onDestroy() {    

        printf("service on destroy");   

        super.onDestroy();    

    }    

    @Override 
    public boolean onUnbind(Intent intent) {  
        printf("service on unbind");  

        return super.onUnbind(intent);  
    }  

    public void onRebind(Intent intent) { 

        printf("service on rebind");   

        super.onRebind(intent);   

    }  

    private void printf(String str) {    

        Log.e("TAG", "###################------ " + str + "------");  
    }  


    void callback(int val) {    

        final int N = mCallbacks.beginBroadcast();   

        for (int i=0; i<N; i++) {    

            try {   mCallbacks.getBroadcastItem(i).actionPerformed(val);    

            } catch (RemoteException e) {  

                // The RemoteCallbackList will take care of removing  

                // the dead object for us.  
            }  

        }    

        mCallbacks.finishBroadcast();    

    }  

  private final ITaskBinder.Stub mBinder = new ITaskBinder.Stub() {  

        public void stopRunningTask() {   

        }    

        public boolean isTaskRunning() {  
            return false;  
        }   

        public void registerCallback(ITaskCallback cb) {   

            if (cb != null) mCallbacks.register(cb);  

        }  

 

        public void unregisterCallback(ITaskCallback cb) {   

            if (cb != null) mCallbacks.unregister(cb);  

        }   

    };         

    final RemoteCallbackList<ITaskCallback> mCallbacks   
        = new RemoteCallbackList<ITaskCallback>();   

}  

 

   

 

4. MyActivity.java  
 

package com.cmcc.demo;   

import android.app.Activity; 
import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.ServiceConnection;  
import android.graphics.Color;  
import android.os.Bundle;  
import android.os.IBinder;  
import android.os.RemoteException;  
import android.util.Log;  
import android.view.View;  
import android.view.ViewGroup;  
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;    
import android.widget.Button;  
import android.widget.LinearLayout;  
import android.widget.RelativeLayout;
import android.widget.TextView;    
import java.io.BufferedReader;  
import java.io.File;    
import java.io.FileOutputStream;    
import java.io.FileReader;    
import java.io.PrintWriter;  
public class MyActivity extends Activity { 

    private Button btnOk;   

    private Button btnCancel;    

    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle);  
        setContentView(R.layout.test_service);  

        btnOk = (Button)findViewById(R.id.btn_ok);    

        btnCancel = (Button)findViewById(R.id.btn_cancel);    

        btnOk.setText("Start Service");    

        btnCancel.setTag("Stop Service");   

        btnOk.setOnClickListener(new OnClickListener() {    

            public void onClick(View v) {    

                onOkClick();   

            }    

        });  

        btnCancel.setOnClickListener(new OnClickListener() {   

            public void onClick(View v) {    

                onCancelClick();    

            }   

        });    

    }  


    void onOkClick() {    

        Bundle args = new Bundle();    

        Intent intent = new Intent(this, MyService.class); 

        intent.putExtras(args);    

        //printf("send intent to start");  

        //startService(intent);  

        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);  

        startService(intent);  

    }  


    void onCancelClick() {  

        Intent intent = new Intent(this, MyService.class);    

        //printf("send intent to stop");    

        unbindService(mConnection);  
        //stopService(intent);    
    }  

    private void printf(String str) {  
        Log.e("TAG", "###################------ " + str + "------");    

    }  


   ITaskBinder mService;  
 

  private ServiceConnection mConnection = new ServiceConnection() {  

        public void onServiceConnected(ComponentName className,  

                IBinder service) { 

            mService = ITaskBinder.Stub.asInterface(service);  
            try {  
                mService.registerCallback(mCallback); 

            } catch (RemoteException e) {  
            }  

        }  

 

      public void onServiceDisconnected(ComponentName className) {   

            mService = null;    

        }    

    };  

    private ITaskCallback mCallback = new ITaskCallback.Stub() {

        public void actionPerformed(int id) {    

            printf("callback id=" + id);    

        }    

    };   


热点排行