android callRemoteService 調用其他進程的service

來源:互聯網
上載者:User

建立一個進程服務.程式第一次啟動並執行時候就建立.不需要介面

要想其他進程能夠跨進程的調用本進程的方法.就得使用跨進程訪問的介面類型.只需將介面中類和方法的修飾符去掉就行.然後把介面類編程aidl類型

服務也業務代碼:


[java] 
package com.example.remoteservice; 
 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.os.RemoteException; 
 
/**
 * 跨進程訪問的介面類型:將介面和介面中方法的修飾類型去掉,
 * android產生aidl檔案步驟:找到編寫的java源檔案,將其副檔名該為aidl.然後回到項目
 * ,重新整理項目,該類就會變為源檔案名稱.aidl的檔案,並且系統會自動在gen
 * .工程目錄下產生原java檔案的名的java檔案.可以開啟看下內容與之前不一樣了.到此就完成了aidl的建立
 * 
 * @author Administrator
 * 
 */ 
public class RemoteService extends Service { 
 
    @Override 
    public IBinder onBind(Intent arg0) { 
        return new Mybinder(); 
    } 
 
    /**
     * 可以繼承系統根據自己定義的iservice介面產生iservice.adil之後的IServcice.Stub類,
     * Stub是系統自己家上的.該類是個抽象類別,繼承了binder類也實現了iservice<br>
     * 
     * @author Administrator
     * 
     */ 
    public class Mybinder extends IServcice.Stub { 
 
        @Override 
        public void callMethodInService() throws RemoteException { 
            sayHelloInService(); 
        } 
 
    } 
 
    /**
     * 服務中的方法
     */ 
    public void sayHelloInService() { 
        System.out.println("hello in service"); 
    } 
 
    @Override 
    public void onCreate() { 
        System.out.println("remote service create"); 
    } 
 
} 
服務進程對外提供調用的介面,將該介面轉成aidl檔案,
aidl檔案的建立:找到編寫的java源檔案,將其副檔名該為aidl.然後回到項目,重新整理項目,該類就會變為源檔案名稱.aidl的檔案,並且系統會自動在gen .工程目錄下產生原java檔案的名的java檔案.可以開啟看下內容與之前不一樣了.到此就完成了aidl的建立


[html] 
package com.example.remoteservice; 
 
interface IServcice { 
    void callMethodInService(); 
} 

在設定檔application中註冊該服務

[html]
<service android:name=".RemoteService" > 
           <intent-filter> 
               <action android:name="com.example.remoteservice" /> <!-- 指定action,可方便別的程式調用 --> 
 
           </intent-filter> 
       </service> 

到此被調用的服務就建立好了.如果想對外使用.必須啟動該進程.和服務.所以.為配合下面調用的例子(調用外部進程的服務)必須保證該進程的正常運行.
總結以上調用本進程中服務的方法:

 1建立一個服務,該服務中要有能被調用的方法;
 2:定義一個借口IService, 介面裡面的抽象方法去調用服務中的方法 ;
 3:定義一個MyBinder對象.繼承binder類,並實現IService介面,在onBind方法中返回MyBinder對象;
 4:在activity中通過bindService ()綁定一個服務;
 5:建立一個MyConn實現ServiceConnection介面 ,服務綁定成功後執行介面中的onServiceConnected()方法;
 6:onServiceConnected ()方法的IBinder參數就是IService類型的,強制轉換為IService;
 7:調用ISservice中方法

調用外部進程中服務(上面的進程)

訪問其他進程中服務的步驟:

 訪問外部服務.要知道對方服務的action,和可以調用服務介面的adil檔案以及adil檔案的包名.在本地程式建立調用服務的aidl檔案的包,並添加(copy)外部服務的aidl檔案,系統會自動在gen目錄下建立該adil檔案介面java檔案

 1建立一個服務,該服務中要有能被調用的方法;
 2:定義一個借口IService, 介面裡面的抽象方法去調用服務中的方法,去掉定義介面和介面中方法的修飾符,把這個介面的副檔名該為.aidl,;
 3:定義一個MyBinder對象,繼承編譯器根據IService.aidl在gen中建立的IService類中的Sutb類,
 在onBind方法中返回MyBinder對象
 4:在調用服務的工程中建立包,包名要與被調用服務工程中對應aidl檔案所在的包名一樣.並複製被調用工程對應的aidl檔案到本地對應aidl檔案包中
 5:在activity中通過bindService ()綁定一個服務;
 6:建立一個MyConn實現ServiceConnection介面 ,服務綁定成功後執行介面中的方法;
 7:在實現介面中的onServiceConnected()方法調用IServcice.Stub.asInterface()方法.將onServiceConnected的IBinder參數轉換成IService類型;
 8:調用IService中的方法(調用服務中的方法),

 

按照上面步驟建立了同樣的包和copy了被調用的aidl檔案之後,建立調用外部進程頁面的布局.本例只以一個button為例


[html] 
<Button 
    android:id="@+id/call" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="click" 
    android:text="調用外部服務" /> 


調用外部服務的代碼實現

[java]
package com.example.callremoteservice; 
 
import com.example.remoteservice.IServcice; 
 
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.view.View; 
 
/**
 * 訪問外部服務.要知道對方服務的action,和可以調用服務介面的adil檔案以及adil檔案的包名<br>
 * 在本地程式建立調用服務的adil檔案的包,並添加外部服務的adil檔案,系統會自動在gen目錄下建立該adil檔案介面java檔案<br>
 * 總結:<br>
 * 訪問本進程中的service,需要用到bindService()方法.(cz23_serverlifecycle)<br>
 * 
 * 1建立一個服務,該服務中要有能被調用的方法;<br>
 * 2:定義一個借口IService, 介面裡面的抽象方法去調用服務中的方法 ;<br>
 * 3:定義一個MyBinder對象.繼承binder類,並實現IService介面,在onBind方法中返回MyBinder對象;<br>
 * 4:在activity中通過bindService ()綁定一個服務;<br>
 * 5:建立一個MyConn實現ServiceConnection介面 ,服務綁定成功後執行介面中的onServiceConnected()方法;<br>
 * 6:onServiceConnected ()方法的IBinder參數就是IService類型的,強制轉換為IService;<br>
 * 7:調用ISservice中方法<br>
 * <br>
 * 訪問 外部進程中的service,需要用到aidl(android interface definition language)(在編號24的兩個例子)<br>
 * 1建立一個服務,該服務中要有能被調用的方法;<br>
 * 2:定義一個借口IService, 介面裡面的抽象方法去調用服務中的方法,去掉定義介面和介面中方法的修飾符,把這個介面的副檔名該為.aidl,;<br>
 * 3:定義一個MyBinder對象,繼承編譯器根據IService.aidl在gen中建立的IService類中的Sutb類,
 * 在onBind方法中返回MyBinder對象<br>
 * 4:在調用服務的工程中建立包,包名要與被調用服務工程中對應aidl檔案所在的包名一樣.並複製被調用工程對應的aidl檔案到本地對應aidl檔案包中<br>
 * 5:在activity中通過bindService ()綁定一個服務;<br>
 * 6:建立一個MyConn實現ServiceConnection介面 ,服務綁定成功後執行介面中的方法;<br>
 * 7:在實現介面中的onServiceConnected()方法調用IServcice.Stub.asInterface()方法.
 * 將onServiceConnected的IBinder參數轉換成IService類型<br>
 * 8:調用IService中的方法(調用服務中的方法),
 * 
 * @author Administrator
 * 
 */ 
public class MainActivity extends Activity { 
    private IServcice iServcice; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        Intent intent = new Intent(); 
        intent.setAction("com.example.remoteservice");// 隱式意圖,指定訪問服務的action,就是被調用服務進程值註冊服務的時候指定的action的值 
        bindService(intent, new MyConn(), Context.BIND_AUTO_CREATE);// 通過隱式意圖綁定到外部服務.參數2是服務綁定成功後的一個回調類.通過他可以擷取外部服務對外提供資料的對象 
    } 
 
    public void click(View view) { 
        try { 
            iServcice.callMethodInService(); 
        } catch (RemoteException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
 
    class MyConn implements ServiceConnection { 
 
        @Override 
        /**
         * 參數IBinder就是綁定被調用的服務執行onBind的傳回值
         */ 
        public void onServiceConnected(ComponentName arg0, IBinder service) { 
            iServcice = IServcice.Stub.asInterface(service);// 調用外部進程中service的轉換方法 
        } 
 
        @Override 
        public void onServiceDisconnected(ComponentName arg0) { 
            // TODO Auto-generated method stub 
 
        } 
 
    } 
} 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.