2010-09-07 51 views

回答

0

步驟#1:發佈AIDL文件(例如從您的網站下載)。

步驟2:向清單中的服務添加一個<intent-filter>,宣傳您打算支持識別服務的某個名稱(例如,自定義操作)。與AIDL一起記錄。

步驟#3:沒有步驟#3。 :-)

下面是一個示例clientservice,演示了這種模式。

0
package com.demo.example.ShareService; 

interface IShareService { 
    boolean isSameProcess(int clientPid); 
} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.pluralsight.example.ShareService" 
      android:sharedUserId="demo.example.ShareServiceUser"> 
    <uses-sdk android:minSdkVersion="17" 
       android:targetSdkVersion="19"/> 
    <application android:icon="@drawable/icon" 
       android:label="@string/app_name" 
       android:process="demo.example.ShareServiceProcess" > 
     <service android:name=".ShareServiceSample" 
       android:exported="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.ACTION_MAIN" /> 
       <action android:name="android.intent.action.RUN" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </service> 
    </application> 
</manifest> 

package com.demo.example.ShareService; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.os.Process; 
import android.util.Log; 

public class ShareServiceSample extends Service { 
    private static final String   LOG_TAG = "ShareServiceSample"; 

    private ShareServiceImpl mBinder = new ShareServiceImpl(); 

    public IBinder onBind(Intent intent) { 
     Log.d(LOG_TAG, "Intent: " + intent.toString() + ", return binder " + mBinder.toString()); 
     return mBinder; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    private class ShareServiceImpl extends IShareService.Stub { 

     public boolean isSameProcess(int clientPid) { 
      boolean    same; 

      same = (clientPid == Process.myPid()); 

      Log.d(LOG_TAG, 
        "Client PID/Service PID: " + 
        Integer.toString(clientPid) + 
        "/" + 
        Integer.toString(Process.myPid())); 
      return same; 
     } 
    } 
}