前篇文章:使用android中的AIDL讓Service與Activity通信(service回調activity)介紹了通過一個回調方式調用service中的方法,其實不用那麼麻煩…只要注冊一個service,在service中的onBind方法中返回一個Bind即可.
例:在Activity中注冊service,通過點擊一個按鈕調用被注冊service中的方法.
代碼:
首先是AIDL:
- package com.zhang.test.service;
- interface IService {
- void printStr(String str);
- }
復制代碼
接下來Service:
- package com.zhang.test.service;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.util.Log;
- public class MainService extends Service {
- private static final String TAG = "MainService";
- //在這裡聲明IService的Binder
- private IService.Stub mBinder = new IService.Stub() {
- @Override
- public void printStr(String str) throws RemoteException {
- print(str);
- }
- };
- private void print(String str) {
- Log.d(TAG, "str:" + str);
- }
- @Override
- public IBinder onBind(Intent intent) {
- Log.d(TAG, "onBind");
- return mBinder;
- }
- @Override
- public void onCreate() {
- Log.d(TAG, "onCreate");
- super.onCreate();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- }
復制代碼
Activity:
- package com.zhang.test;
- 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.view.VIEw.OnClickListener;
- import android.widget.Button;
- import com.zhang.test.service.IService;
- import com.zhang.test.service.MainService;
- public class MainActivity extends Activity {
- private static final String TAG = "MainActivity";
- private IService mService;
- private Button mButton;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentVIEw(R.layout.main);
- Intent i = new Intent(this, MainService.class);
- //綁定Service,需要一點時間去內存中查找這個service,因此不能在bindService後直接調用service的方法
- bindService(i, mConnection, Context.BIND_AUTO_CREATE);
- mButton = (Button) findVIEwById(R.id.Button01);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(VIEw v) {
- if(mService != null) {
- try {
- mService.printStr("haha");
- } catch (RemoteException e) {
- Log.e(TAG, "", e);
- }
- }
- }
- });
- }
- @Override
- protected void onDestroy() {
- unbindService(mConnection);
- super.onDestroy();
- }
- /**
- * 注冊connection
- */
- private ServiceConnection mConnection = new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Log.d(TAG, "onServiceDisconnected");
- mService = null;
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Log.d(TAG, "onServiceConnected");
- mService = IService.Stub.asInterface(service);
- }
- };
- }
復制代碼
最後是Manifest:
- <manifest XMLns:android="http://schemas.android.com/apk/res/android" package="com.zhang.test" android:versioncode="1" android:versionname="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN">
- <category android:name="android.intent.category.LAUNCHER">
- </category>
- </action>
- <service android:name=".service.MainService">
- </service>
- <uses-sdk android:minsdkversion="3">
- </uses-sdk>
- </intent-filter></activity></application></manifest>