1)应用场景:后台运行程序的时候,就要使用到Service。Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行。比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以 用Service在后台定时更新,而不用每打开应用的时候再去获取。
2)启动停止流程:Service跟Activities是不同的(可以理解为后台与前台的区别),例如,如果需要使用Service的话,需要调用startService(),从而利用 startService()去调用Service中的OnCreate()和onStart()方法来启动一个后台的Service
- 启动一个Service的过程如下:context.startService() ->onCreate()- >onStart()->Service running其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。(注意:如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法
- 停止一个Service的过程如下:context.stopService() | ->onDestroy() ->Service stop
3) 编写应用层service
首先service 继承自Service,通过Intent进行启动即可,使用时在AndroidManifest.xml进行声明及权限设定
public class _Service extends Service
{
//定义个一个Tag标签
private static final String TAG = "_Service";
//这里定义吧一个Binder类,用在onBind()有方法里,这样Activity那边可以获取到
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
_Service getService() {
return _Service.this;
}
}
@Override
public IBinder onBind(Intent intent)
{
Log.e(TAG, "start IBinder~~~");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created…", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed…", Toast.LENGTH_LONG).show();
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "Service onUnbind…", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
AndroidMainfest.xml文件中增加:
<service android:name=".net._Service"></service>
应用程序嗲用service方法:
方法一:通过context.startService来启动Service。
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.ice.net._Service");
startService(serviceIntent);
serviceIntent.setAction("com.ice.net._Service");
startService(serviceIntent);
方法二:
/** Flag indicating whether we have called bind on the service. */
private boolean mBound;
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName className, IBinder service)
{
_Service.LocalBinder binder = (_Service.LocalBinder) service;
private boolean mBound;
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName className, IBinder service)
{
_Service.LocalBinder binder = (_Service.LocalBinder) service;
binder.getxxx();//调用具体方法
mBound = true;
}
public void onServiceDisconnected(ComponentName className)
{
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mBound = false;
}
};
@Override
protected void onStart()
{
super.onStart();
// Bind to the service
Intent intent = new Intent(this, _Service.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
protected void onStart()
{
super.onStart();
// Bind to the service
Intent intent = new Intent(this, _Service.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop()
{
super.onStop();
// Unbind from the service
if (mBound)
{
unbindService(mConnection);
mBound = false;
}
}
{
super.onStop();
// Unbind from the service
if (mBound)
{
unbindService(mConnection);
mBound = false;
}
}
4)编写系统层service
待续。。。
今天发现
http://blog.csdn.net/nkmnkm/article/details/7307462对android Service讲的比较详细,直接学习这个吧。