Android组件--service

一. service与activity通信

1. 同进程下

在service中自定义binder, 在activity中绑定service时得到此binder实例, 通过该实例实现两者间的通信.

public class MainActivity extends Activity { 

    private static final String TAG = "cc"; 
    public MyBinder mBinder; 

    private ServiceConnection mConnection = new ServiceConnection() { 
        @Override 
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 
            //在Activity里面取得Service里的binder对象 
            mBinder = (MyBinder)iBinder; 
            //注册自定义回调 
            mBinder.setOnTestListener(new MyBinder.OnTestListener() { 
                @Override 
                public void onTest(String str) { 
                    Log.d(TAG, "receive msg from service: "+str); 
                } 
            }); 
        } 

        @Override 
        public void onServiceDisconnected(ComponentName componentName) { 

        } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        Intent intent = new Intent(MainActivity.this, MyService.class); 
        bindService(intent,mConnection,BIND_AUTO_CREATE); 

        findViewById(R.id.test_bt).setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                //点击按钮调用mBinder里面的方法,发送消息给Service 
                mBinder.testMethod("hi, service."); 
            } 
        }); 
    } 
}


public class MyService extends Service { 
    private static final String TAG = "cc"; 
    // 实例化一个MyBinder对象 
    private MyBinder mBinder = new MyBinder(this); 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
        return mBinder;//返回这个mBinder对象 
    } 

    public void serviceMethod(String str){ 
        Log.d(TAG, "receive msg from activity: " + str); 
    } 
}


public class MyBinder extends Binder { 
    private static final String TAG = "cc"; 
    private MyService mService; 
    private OnTestListener mListener; 

    public MyBinder(MyService service) { 
        this.mService = service; 
    } 

    public void testMethod(String str) { 
        // Activity通过Binder来调用Service的方法将消息传给Service 
        mService.serviceMethod(str); 
        // 并回调mListener.onTest告诉Activity已收到消息 
        mListener.onTest("hi, activity."); 
    } 

    // MyBinder 里面提供一个注册回调的方法 
    public void setOnTestListener(OnTestListener listener) { 
        this.mListener = listener; 
    } 

    //自定义一个回调接口 
    public interface OnTestListener { 
        void onTest(String str); 
    } 
}
2. 跨进程下

使用AIDL跨进程通信, 整体过程和单进程一样, 都是通过一个Binder来通信的, 区别在于单进程的Binder是自己通过继承Binder类来手动实现的, 而跨进程的Binder是通过AIDL自动生成的. 简单来说, AIDL就是一个用来生成代码的工具, 最终的目的就是得到IMyAidlInterface.java这个类.

interface IMyAidlInterface { 
    void testMethod(String str); 
}
public class AidlBinder extends IMyAidlInterface.Stub { 

    private MyService mService; 

    public AidlBinder(MyService service) { 
        this.mService = service; 
    } 

    @Override 
    public void testMethod(String str) throws RemoteException { 
        mService.serviceMethod(str); 
    } 
}
ublic class MyService extends Service { 

    private static final String TAG = "zjy"; 

    private AidlBinder mBinder = new AidlBinder(this); 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
        return mBinder; 
    } 

    public void serviceMethod(String str) { 
        Log.d(TAG, "receive msg from activity: " + str); 
    } 
}
public class MainActivity extends Activity { 

    private static final String TAG = "zjy"; 
    public IMyAidlInterface mService; 

    private ServiceConnection mConnection = new ServiceConnection() { 
        @Override 
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 
            mService = IMyAidlInterface.Stub.asInterface(iBinder); 
        } 

        @Override 
        public void onServiceDisconnected(ComponentName componentName) { 

        } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        Intent intent = new Intent(MainActivity.this, MyService.class); 
        bindService(intent, mConnection, BIND_AUTO_CREATE); 

        findViewById(R.id.test_bt).setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View view) { 
                try { 
                    mService.testMethod("hi, service."); 
                } catch (RemoteException e) { 
                    e.printStackTrace(); 
                } 
            } 
        }); 
    } 
}
interface IMyCallbackListener { 
    void onRespond(String str); 
}
import com.zjy.servicedemo.IMyCallbackListener; 

interface IMyAidlInterface { 
    void testMethod(String msg); 
    void registerListener(IMyCallbackListener listener); 
}
public class AidlBinder extends IMyAidlInterface.Stub { 

    private MyService mService; 
    private IMyCallbackListener mListener; 

    public AidlBinder(MyService service) { 
        this.mService = service; 
    } 

    @Override 
    public void testMethod(String str) throws RemoteException { 
        mService.serviceMethod(str); 
        mListener.onRespond("hi, activity"); 
    } 

    @Override 
    public void registerListener(IMyCallbackListener listener) throws RemoteException { 
        mListener = listener; 
    } 
}
try{ 
    mService.registerListener(new IMyCallbackListener.Stub() { 
        @Override 
        public void onRespond(String str) throws RemoteException { 
            Log.d(TAG, "receive message from service: "+str); 
        } 
    }); 
} catch (RemoteException e){ 
    e.printStackTrace(); 
}

你可能感兴趣的:(Android组件--service)