安卓AIDL进口 [英] android aidl import
问题描述
我试图导入android.content.Context到AIDL文件,但月食不承认它。
I'm trying to import android.content.Context to AIDL file but eclipse doesn't recognize it..
这是我的code:
package nsip.net;
import android.content.Context; // error couldn't find import for class ...
interface IMyContactsService{
void printToast(Context context, String text);
}
谁能帮我?
推荐答案
使用 android.content.Context
是行不通的,因为它没有实施 android.os.Parcelable
。
Using android.content.Context
isn't going to work since it doesn't implement android.os.Parcelable
.
不过 - FF你有一个类( MyExampleParcelable
为例)要在一个AIDL接口传输(安培;真正实现 Parcelable
)创建一个 .aidl
文件, MyExampleParcelable.aidl
中这样写:
However - ff you have a class (MyExampleParcelable
for instance) that you want to transfer in an AIDL interface (& that actually implements Parcelable
) you create an .aidl
file, MyExampleParcelable.aidl
in which you write:
package the.package.where.the.class.is;
parcelable MyExampleParcelable;
现在,除非你拼命想要跨进程谈谈你应该考虑当地的服务。
修改(稍微有所帮助):
Now, unless you desperately want to talk across processes you should consider local services.
Edit(slightly more helpful):
这是一个本地服务(即它仅用于自己的应用程序和进程内)?在这种情况下,它通常只是更好地执行粘合剂,并直接返回。
Is this a local service (i.e. it will only be used inside your own application & process)? In these cases it's usually just better to implement a binder and return that directly.
public class SomeService extends Service {
....
....
public class SomeServiceBinder extends Binder {
public SomeService getSomeService() {
return SomeService.this;
}
}
private final IBinder mBinder = new SomeServiceBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public void printToast(Context context, String text) {
// Why are you even passing Context here? A Service can create Toasts by it self.
....
....
}
// And all other methods you want the caller to be able to invoke on
// your service.
}
基本上,当活动
已绑定到你的服务就会简单地把得到的的IBinder
到 SomeService.SomeServiceBinder
,呼叫 SomeService.SomeServiceBinder#getSomeService()
- 和 嘭 ,访问正在运行的服务
实例+,你可以在它的API调用的东西。
Basically, when the Activity
has bound to your service it will simply cast the resulting IBinder
to SomeService.SomeServiceBinder
, call SomeService.SomeServiceBinder#getSomeService()
- and bang, access to the running Service
instance + you can call stuff in its API.
这篇关于安卓AIDL进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!