Activity用于显示东西和用户交互,那么Sercive就是不显示东西,运行在后台的组件了。
创建Service类:
首先继承Service类,实现它的方法,onBind()必须实现,另外还有一些它生命周期会调用的函数,onCreate(),onStart(),onDestroy(),onUnbind(),比起Activity少呢,另外还要在AndroidManifest.xml中声明它。
public class MyService extends Service
{
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
Log.e("MyService", "onBind----------");
Toast toast=Toast.makeText(MyService.this, "onBind------", Toast.LENGTH_LONG);
toast.show();
return myBinder;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
Log.e("MyService", "onCreate----------");
Toast toast=Toast.makeText(MyService.this, "onCreate------", Toast.LENGTH_LONG);
toast.show();
super.onCreate();
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
Log.e("MyService", "onDestroy----------");
Toast toast=Toast.makeText(MyService.this, "onDestroy------", Toast.LENGTH_LONG);
toast.show();
super.onDestroy();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
Log.e("MyService", "onStart----------");
Toast toast=Toast.makeText(MyService.this, "onStart------", Toast.LENGTH_LONG);
toast.show();
super.onStart(intent, startId);
}
@Override
public boolean onUnbind(Intent intent)
{
// TODO Auto-generated method stub
Log.e("MyService", "onUnbind----------");
Toast toast=Toast.makeText(MyService.this, "onUnbind------", Toast.LENGTH_LONG);
toast.show();
return super.onUnbind(intent);
}
}
</pre><pre name="code" class="html"><service
android:name=".MyService">
<intent-filter >
<action android:name="Hello"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
启动Service:
(1)利用startService(Intent)和stopService(Intent)
利用这种方式启动Service的生命周期是onCreate()---->onStart()----->onDestroy(),而且生命周期不会随Activity结束而消亡。
Intent intent=new Intent();
intent.setAction("Hello");
MainActivity.this.startService(intent);
Intent intent=new Intent();
intent.setAction("Hello");
MainActivity.this.stopService(intent);
(2)利用bindService(Intent,ServiceConnection,Service.Bind_AUTO_CREATE)和unbindService(ServiceConnection)
利用这种方式启动Service的生命周期是onCreate()----->onBind()------>onUnbind()-------->onDestroy(),生命周期和Activity绑定在一起共存亡,绑定的好处在于Activity能够访问到Service的数据ServiceConnection类:
它里面有public void onServiceConnected(ComponentName arg0, IBinder arg1);和public void onServiceDisconnected(ComponentName arg0);
这2个函数分别在成功绑定是回调,因异常Activity关闭回调
bindService(intent,ServiceConnection,Service,BInd_AUTO_CREATE)这种启动Service方式onCreate()---->onBind()这个函数会返回IBinder对象作为参数传给onServiceConnected()函数,然后可以保存这个由Service传过来的参数,用于数据交换。
在Service类中创建一个BInder对象:
private int count=0;
private boolean flag=false;
class MyBinder extends Binder
{
public int getCount()
{
return count;
}
}
Service类的onBind(),返回了这个Binder对象,这个对象的方法可以获得Service类的count成员的值。
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
Log.e("MyService", "onBind----------");
Toast toast=Toast.makeText(MyService.this, "onBind------", Toast.LENGTH_LONG);
toast.show();
return myBinder;
}
onBInd()函数完成之后,回调onServiceConnected(ComponentName arg0, IBinder arg1);那么首先在Activity那里实现ServiceConnection对象先:
ServiceConnection cn=new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName arg0)
{
// TODO Auto-generated method stub
Log.e("ServiceConnection", "断开连接");
Toast toast=Toast.makeText(MainActivity.this, "onServiceDisconnection----", Toast.LENGTH_LONG);
toast.show();
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1)
{
// TODO Auto-generated method stub
Log.e("ServiceConnection", "连接成功");
Toast toast=Toast.makeText(MainActivity.this, "onServiceConnection----", Toast.LENGTH_LONG);
toast.show();
binder=(MyService.MyBinder)arg1;
}
};
最后才慢慢启动Service:
Intent intent=new Intent();
intent.setAction("Hello");
MainActivity.this.bindService(intent, cn, Service.BIND_AUTO_CREATE);
Intent intent=new Intent();
intent.setAction("Hello");
MainActivity.this.unbindService(cn);