如何与其他APK共享服务界面?

时间:2010-09-07 02:44:31

标签: android

我正在尝试创建一个使用AIDL生成的接口的服务。如何使界面可用于其他APK中的应用程序?

谢谢!

2 个答案:

答案 0 :(得分:0)

步骤1:发布AIDL文件(例如,从您的网站下载)。

步骤2:向清单中的服务添加<intent-filter>,宣传您计划支持的某些名称(例如,自定义操作)以识别您的服务。将此文件与AIDL一起记录下来。

步骤3:没有第3步。 : - )

以下是演示此模式的示例clientservice

答案 1 :(得分: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;
        }
    }
}