android 本地服务,Android本地服务示例,bindservice()和ServiceConnection()

我有一个问题与@mnish大约一年前提出的这个question有关.

请看看他的问题和代码.他实现了一个ServiceConnection()并将其传递给bindService().这遵循顶部附近的Service文档中的本地服务示例.

我想实现本地服务示例,所以我试图从@mnish问题/答案中添加一些细节.在ServiceConnection()中@mnish有这条线让我感到困惑:

mService = ILocService.Stub.asInterface(iservice);

我理解@mnish编写了这段代码,但有没有人知道ILocService是什么,并且知道如何创建自己的ILocService?这个结构记录在哪里,我需要它吗? IBinder的价值来自何处?

解决方法:

因此,他必须使用服务器端实现的存根,如记录:

// This is called when the connection with the service has been

// established, giving us the service object we can use to

// interact with the service. We are communicating with our

// service through an IDL interface, so get a client-side

// representation of that from the raw service object.

mService = IRemoteService.Stub.asInterface(service);

iservice引用来自onServiceConnected方法,该方法在将服务绑定到您的活动之后调用.调用bindService传递ServiceConnection,它实现onServiceConnected方法.

当您的服务实现是本地的时,您不需要“IRemoteService.Stub.asInterface(service)”,那么您可以将服务转发给您本地服务.

本地服务示例在服务中执行此操作:

public class LocalService extends Service {

private NotificationManager mNM;

// Unique Identification Number for the Notification.

// We use it on Notification start, and to cancel it.

private int NOTIFICATION = R.string.local_service_started;

/**

* Class for clients to access. Because we know this service always

* runs in the same process as its clients, we don't need to deal with

* IPC.

*/

public class LocalBinder extends Binder {

LocalService getService() {

return LocalService.this;

}

}

...

}

这在ServiceConnection类的Activity中:

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className, IBinder service) {

// This is called when the connection with the service has been

// established, giving us the service object we can use to

// interact with the service. Because we have bound to a explicit

// service that we know is running in our own process, we can

// cast its IBinder to a concrete class and directly access it.

mBoundService = ((LocalService.LocalBinder)service).getService();

// Tell the user about this for our demo.

Toast.makeText(Binding.this, R.string.local_service_connected,

Toast.LENGTH_SHORT).show();

}

public void onServiceDisconnected(ComponentName className) {

// This is called when the connection with the service has been

// unexpectedly disconnected -- that is, its process crashed.

// Because it is running in our same process, we should never

// see this happen.

mBoundService = null;

Toast.makeText(Binding.this, R.string.local_service_disconnected,

Toast.LENGTH_SHORT).show();

}

};

标签:android-binder,stub,android,android-service

来源: https://codeday.me/bug/20190730/1577971.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值