Android怎么實現(xiàn)在ServiceManager中加入自定義服務(wù)的方法

這篇文章主要介紹了Android怎么實現(xiàn)在ServiceManager中加入自定義服務(wù)的方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)公司成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計,成都響應(yīng)式網(wǎng)站建設(shè)公司,網(wǎng)頁設(shè)計師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:13518219792

具體如下:

當(dāng)我們要使用android的系統(tǒng)服務(wù)時,一般都是使用Context.getSystemService方法。例如我們要獲取AudioManager,我們可以:

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

獲取的服務(wù),其實是在ServiceManager中注冊的Binder服務(wù),然后進(jìn)行封裝后,提供給用戶。

可以看ContextImpl.java中的實現(xiàn):

static {
    ......
    // 將AudioManager加入SYSTEM_SERVICE_MAP中,調(diào)用getSystemService時,
    // 就會從SYSTEM_SERVICE_MAP得到AudioManager
    registerService(AUDIO_SERVICE, new ServiceFetcher() {
        public Object createService(ContextImpl ctx) {
          return new AudioManager(ctx);
        }});
    ......
}

AudioManager是對IAudioService的封裝,實際操作都是使用IAudioService進(jìn)行的,看AudioManager中的代碼:

private static IAudioService getService()
{
    if (sService != null) {
      return sService;
    }
    // 從ServiceManager中獲取Binder
    IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
    // 將Binder轉(zhuǎn)化成IAudioService,方便調(diào)用
    sService = IAudioService.Stub.asInterface(b);
    return sService;
}

上面是android系統(tǒng)的使用方式。如果我們添加自己的服務(wù),要如何做呢?

我們在eclipse中建3個測試工程:

1)MyServiceLib:這是個lib工程,需要在eclipse中勾選Is Library。后面的兩個工程,都需要將MyServiceLib添加到Library中。

2) MyService: 用于在android開機(jī)時注冊自定義服務(wù)進(jìn)ServiceManager。因為ServiceManager被@hide隱藏了,所以要使用它需要自己手動添加sdk包,添加方式可參考在Eclipse中使用SDK中@hide函數(shù)的方法附加說明。另外,添加服務(wù),需要System用戶,所以manifest文件中需要加上android:sharedUserId="android.uid.system", 并且要使用platform簽名簽名apk。

3)MyServiceTest:用于測試上面兩個工程。

下面我們就來編碼。

先在MyServiceLib工程中創(chuàng)建一個aidl文件,android編譯工具會幫我們生成相應(yīng)的java類,aidl文件如下

package com.test.lib;
interface IMyService {
  void setValue(int val);
  int getValue();
}

定義了兩個接口用于測試,setValue和getValue。
android編譯工具會幫我們在gen目錄下生成一個IMyService的java類。

2. 在MyService工程中創(chuàng)建MyService類, 這個類繼承自IMyService.Stub,實現(xiàn)了setValue和getValue接口,這就是一個Service。

package com.test.myservice;
import android.os.RemoteException;
import com.test.lib.IMyService;
public class MyService extends IMyService.Stub {
  private int value;
  @Override
  public void setValue(int val) throws RemoteException {
    this.value = val;
  }
  @Override
  public int getValue() throws RemoteException {
    return value;
  }
}

下面我們將把它加入至ServiceManager中。

3. 在MyService工程中創(chuàng)建MyServiceApplication類

package com.test.myservice;
import android.app.Application;
import android.os.ServiceManager;
public class MyServiceApplication extends Application{
  @Override
  public void onCreate() {
    super.onCreate();
    ServiceManager.addService("MYSERVICE", new MyService());
  }
}

這是一個Application,我們希望android系統(tǒng)啟動時,就創(chuàng)建這個Application,在onCreate方法中,創(chuàng)建MyService類,并加入到ServiceManager中。因此,我需要修改下manifest文件

<application
    android:name=".MyServiceApplication"  //指定Application為我們創(chuàng)建的MyServiceApplication
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:persistent="true"       // 加上persistent=ture,ActivityManager創(chuàng)建的時候,就會創(chuàng)建該應(yīng)用的進(jìn)程,并調(diào)用MyServiceApplication的onCreate方法
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

注意,這個應(yīng)用需要system用戶,并簽名才可運行。

這樣,服務(wù)端就好了,并且開機(jī)時,我們的服務(wù)就已經(jīng)在ServiceManager中了。

4. 下面我們提供一個Manager類方便客戶端使用。在MyServiceLib中創(chuàng)建MyManager類:

package com.test.lib;
import android.os.RemoteException;
import android.os.ServiceManager;
public class MyManager {
  private static MyManager instance;
  private IMyService myservice;
  public static MyManager getInstance() {
    if (instance == null) {
      instance = new MyManager();
    }
    return instance;
  }
  private MyManager() {
    // 從ServiceManager中獲取服務(wù)
    myservice = IMyService.Stub.asInterface(ServiceManager.getService("MYSERVICE"));
  }
  public void setValue(int value) throws RemoteException {
    myservice.setValue(value);
  }
  public int getValue() throws RemoteException {
    return myservice.getValue();
  }
}

5. 在MyServiceTest工程中進(jìn)行測試

通過MyManager.getInstance()可以很方便的獲取服務(wù)的Manager,對遠(yuǎn)程服務(wù)進(jìn)行調(diào)用。我們創(chuàng)建一個Activity來使用MyManager

package com.test.client;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.test.binder.client.R;
import com.test.lib.MyManager;
public class MainActivity extends Activity implements OnClickListener {
  MyManager myManager;
  Button btnSetValue;
  Button btnGetValue;
  TextView tvValue;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    setContentView(R.layout.activity_main);
    btnSetValue = (Button) findViewById(R.id.btn_set_value);
    btnGetValue = (Button) findViewById(R.id.btn_get_value);
    tvValue = (TextView) findViewById(R.id.tv_value);
    // 獲取MyManager
    myManager = MyManager.getInstance();
  }
  @Override
  public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btn_set_value:
      int value = new Random().nextInt();
      try {
        myManager.setValue(value);
        Toast.makeText(this, "set value to "+value+ " success!", 0).show();
      } catch (RemoteException e) {
        e.printStackTrace();
        Toast.makeText(this, "set value fail!", 0).show();
      }
      break;
    case R.id.btn_get_value:
      try {
        tvValue.setText("value:"+myManager.getValue());
      } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      break;
    default:
      break;
    }
  }
}

附:在Eclipse中使用SDK中@hide函數(shù)的方法

我們使用Eclipse進(jìn)行android開發(fā)時,使用的是ADT中提供的SDK,里面是不包含@hide函數(shù)和變量的。因為android為了兼容、安全等原因,在提供SDK時,把這些函數(shù)給隱藏了。但是,很多時候,我們又需要使用這些函數(shù),因此我們需要手動添加android SDK。例如,當(dāng)我們使用AudioManager時,當(dāng)需要看某種streamType是否mute時,可以調(diào)用isStreamMute(int streamType)這個方法,但是因為它是@hide的,所以我們就需要引入自己的sdk,才能編譯通過。

1. android系統(tǒng)編譯時,當(dāng)編譯“include $(BUILD_JAVA_LIBRARY)”時,會在$ANDROID_SOURCE_BASE/out/target/common/obj/JAVA_LIBRARIES生成中間文件,當(dāng)我們需要使用某些類庫時,可以從這里面找。

isStreamMute(int streamType)在framework.jar中,我們從out/target/common/obj/JAVA_LIBRARIES/framework_intermediates中,將classes.jar拷貝到本地,并重命名為framework.jar。

2. 在eclipse中右鍵工程->Properties->Java Build Path->Libraries->Add External JAR

Android怎么實現(xiàn)在ServiceManager中加入自定義服務(wù)的方法

3. 點擊Order and Export,將framework.jar 置頂

Android怎么實現(xiàn)在ServiceManager中加入自定義服務(wù)的方法

4. 現(xiàn)在,我們就可以使用AudioManager中的isStreamMute(int streamType)方法了

Android怎么實現(xiàn)在ServiceManager中加入自定義服務(wù)的方法

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android怎么實現(xiàn)在ServiceManager中加入自定義服務(wù)的方法”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

本文標(biāo)題:Android怎么實現(xiàn)在ServiceManager中加入自定義服務(wù)的方法
當(dāng)前網(wǎng)址:http://www.muchs.cn/article40/gjgiho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、動態(tài)網(wǎng)站網(wǎng)站策劃、微信公眾號、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)