启动Service

Posted ggzx666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了启动Service相关的知识,希望对你有一定的参考价值。

启动Service:
1.创建一个service类
2.Intent intent=new Intent(this,MyService.class);
startService(intent);

停止:Intent intent2=new Intent(this,MyService.class);
stopService(intent2);

绑定:Intent intent3==new Intent(this,MyService.class);
bindService(intent3,new ServiceConnection()){
@Override
public void on ServiceConnected(ComponentName componentName,Ibinder iBinder)
}
这里我修改了service中的onbind方法,将throw换成了return null,然后bind才不会闪退
解绑:UnBindService
unbindService(scc);//这里的参数需要将

解析:必须要bind才能unbind,否则会报错,因为unbind使用了一个参数,这个参数只有绑定时才会产生
这里启动,停止,绑定,解绑,虽然创建了多个intent,但是都是同一个条件

Ibinder:用于远程操作对象的一个基本接口

以上是关于启动Service的主要内容,如果未能解决你的问题,请参考以下文章

Service使用全解析

初学Android 创建 启动 停止Service 五十八

Android启动init.rc中service的权限问题

Android基础系列合集

Android: 启动init.rc 中service的权限问题转

熊猫 groupby 和总和组