Service的生命周期 Service的生命周期方法只有onCreate, onStart, onDestroy Service是后台进程,不实现任何用户界面。 有耗时的操作还是要放到线程中执行 创建一个Service //继承Service类
public
class
MyService
extends
Service
{
//必须重写
@Override
public
IBinder
onBind
(
Intent
intent
)
{
Log
.
d
(
"MyDebug"
,
"onBind"
);
return
null
;
}
@Override
public
void
onCreate
()
{
super
.
onCreate
();
Log
.
d
(
"MyDebug"
,
"onCreate"
);
}
@Override
public
void
onDestroy
()
{
super
.
onDestroy
();
Log
.
d
(
"MyDebug"
,
"onDestroy"
);
}
@Override
public
void
onRebind
(
Intent
intent
)
{
super
.
onRebind
(
intent
);
Log
.
d
(
"MyDebug"
,
"onRebind"
);
}
@Override
public
void
onStart
(
Intent
intent
,
int
startId
)
{
super
.
onStart
(
intent
,
startId
);
Log
.
d
(
"MyDebug"
,
"onStart"
);
}
@Override
public
boolean
onUnbind
(
Intent
intent
)
{
Log
.
d
(
"MyDebug"
,
"onUnbind"
);
return
super
.
onUnbind
(
intent
);
}
}
使用Service 第一种 <!--
注册 如果有需要用到的service,则都要在这里做相应的配置
-->
<service
android:name
=
".MyService"
></service>
//代码中调用
Intent intent = new Intent(Main.this, MyService.class);
this.startService(intent);
this.stopService(intent);
//绑定Service,conn为ServiceConnection实例
this.bindService(intent, conn, Context.BIND_AUTO_CREATE);
this.unbindService(conn);
第二种 <service
class
=
”.MyService”
>
<intent-filter>
<action
android:value
=
”com.wissen.testApp.service.MY_SERVICE”
/>
</intent-filter>
</service>
Intent serviceIntent = new Intent();
serviceIntent.setAction(”com.wissen.testApp.service.MY_SERVICE”);
//启动Service
this.startService(intent);
绑定Service //绑定需要ServiceConnection实例
ServiceConnection
conn
=
new
ServiceConnection
()
{
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
Log
.
i
(”
INFO
”,
“
Service
bound
“);
}
@Override
public
void
onServiceDisconnected
(
ComponentName
arg0
)
{
Log
.
i
(”
INFO
”,
“
Service
Unbound
“);
}
};
bindService
(
new
Intent
(”
com
.
wissen
.
testApp
.
service
.
MY_SERVICE
”),
conn
,
Context
.
BIND_AUTO_CREATE
);
当应用程序绑定一个Service后,该应用程序和Service之间就能进行互相通信,通常,这种通信的完成依靠于我们定义的一些接口 package
com
.
wissen
.
testApp
;
public
interface
IMyService
{
public
int
getStatusCode
();
}
/*这里定义了一个方法来获取Service的状态,但是Service是如何来使它起作用的呢?之前我们看到
Service中有个返回IBinder对象的 onBind方法,这个方法会在Service被绑定到其他程序上时被调用,
而这个IBinder对象和之前看到的onServiceConnected 方法中传入的那个IBinder是同一个东西。应用和
Service间就依靠这个IBinder对象进行通信:*/
public
class
MyService
extends
Service
{
private
int
statusCode
;
private
MyServiceBinder
myServiceBinder
=
new
MyServiceBinder
();
@Override
public
IBinder
onBind
(
Intent
intent
)
{
return
myServiceBinder
;
}
public
class
MyServiceBinder
extends
Binder
implements
IMyService
{
public
int
getStatusCode
()
{
return
statusCode
;
}
}
}
//下列代码是说明getStatusCode是如何被调用的:
ServiceConnection
conn
=
new
ServiceConnection
()
{
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
IMyService
myService
=
(
IMyService
)
service
;
statusCode
=
myService
.
getStatusCode
();
Log
.
i
(”
INFO
”,
“
Service
bound
“);
}
};
//或者,你也可以通过使用ServiceListener接口来达成相同的目的。
与远程Service通信(进程间Service通信) 如何两个进程间的Service需要进行通信,则需要把对象序列化后进行互相发送。 Android提供了一个 AIDL (Android接口定义语言)工具来处理序列化和通信。 这种情况下Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口, 并且在生成的服务接口中包含一个功能调用的stub服务桩类。 Service的实现类需要去继承这个 stub服务桩类。 Service的onBind方法会返回实现类的对象,之后你就可以使用它了,参见下例: 先创建一个IMyRemoteService.aidl文件,内容如下: package
com
.
wissen
.
testApp
;
interface
IMyRemoteService
{
int
getStatusCode
();
}
/*如果你正在使用eclipse的 Android插件,则它会根据这个aidl文件生成一个Java接口类。生成的接口类中会有一个内部类Stub类,你要做的事就是去继承该Stub类:*/
package
com
.
wissen
.
testApp
;
class
RemoteService
implements
Service
{
int
statusCode
;
@Override
public
IBinder
onBind
(
Intent
arg0
)
{
return
myRemoteServiceStub
;
}
private
IMyRemoteService
.
Stub
myRemoteServiceStub
=
new
IMyRemoteService
.
Stub
()
{
public
int
getStatusCode
()
throws
RemoteException
{
return
0
;
}
};
}
/*当客户端应用连接到这个Service时,onServiceConnected方法将被调用,
客户端就可以获得IBinder对象。参看下面的客户端onServiceConnected方法:*/
ServiceConnection
conn
=
new
ServiceConnection
()
{
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
IMyRemoteService
myRemoteService
=
IMyRemoteService
.
Stub
.
asInterface
(
service
);
try
{
statusCode
=
myRemoteService
.
getStatusCode
();
}
catch
(
RemoteException
e
)
{
// handle exception
}
Log
.
i
(”
INFO
”,
“
Service
bound
“);
}
};
权限 <!--可以在AndroidManifest.xml文件中使用<service>标签来指定Service中android:permission设置访问的权限-->
<service
class
=
”.service.MyService”
android:permission
=
”com.wissen.permission.MY_SERVICE_PERMISSION”
>
<intent-filter>
<action
android:value
=
”com.wissen.testApp.service.MY_SERVICE”
/>
</intent-filter>
</service>
<!--之后应用程序要访问该Service的话就需要使用<user-permission>标签来指定相应的权限-->
<uses-permission
android:name
=
”com.wissen.permission.MY_SERVICE_PERMISSION”
>
</uses-permission>
一个原则是Service的onCreate的方法只会被调用一次,就是你无论多少次的startService又 bindService,Service只被创建一次。 如果先是bind了,那么start的时候就直接运行Service的onStart方法,如果先是start, 那么bind的时候就直接运行onBind方法。如果你先bind上了,就stop不掉了,对啊, 就是stopService不好使了,只能先UnbindService, 再StopService, 所以是先start还是先bind行为是有区别的。 |