標籤:des android style blog http io ar os 使用
Service是Android中的四大組件之一,和windows中的服務是類似,服務一般沒有使用者操作介面,它運行於系統中不容易被使用者發覺,可以使用它開發如監控之類的程式Service,手機中有的程式的更新,服務的推送。Android系統中,Service與Activity類似,都需要AndroidManifest.xml檔案中配置,而且生命週期有點類似。Service不像Activity在前台運行,而是默默的在後台默默的工作,簡單粗暴點理解就是男主內女主外,分工明確。
Service生命週期
service與activity一樣都存在與當前進程的主線程中,所以,一些阻塞UI的操作,比如耗時操作不能放在service裡進行,比如另外開啟一個線程來處理類似下載這種耗時操作。如果在service裡進行一些耗CPU和耗時操作,應用會彈出是強制關閉還是等待的對話方塊。因此service和activity是平級的,四大組件之一的地位也不是浪得虛名。先看張經典老圖:
這兩個都是Service的生命週期,從上到到下也沒有什麼難懂的英文,應該比較好理解,如果有困惑,可以先參考下面的程式就懂了.
兩種啟動Service方式
自訂一個BookService繼承自Service:
public class BookService extends Service {private String tag="BookService";@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Log.i(tag, "開始onCreate啟動了");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.i(tag, "開始執行onStartCommand啟動了");Toast.makeText(this, "BookService開始了",Toast.LENGTH_SHORT).show();return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubLog.i(tag, "銷毀onDestroy啟動了");super.onDestroy();}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i(tag, "綁定onBind啟動了");return null;}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubLog.i(tag, "解除綁定onUnbind啟動了");return super.onUnbind(intent);}}
看下:
布局很簡單,就不貼代碼了,但是需要在AndroidManifest.xml註冊一下代碼:
<service android:name="com.example.googleservice.BookService"></service>
點擊第一個按鈕執行的代碼,Intent之前有寫過,之前是startActivity,這裡用的startService:
Intent service=new Intent(this,BookService.class);startService(service);
第二個按鈕執行的事件:
Intent stopservice=new Intent(this,BookService.class);stopService(stopservice);
通過Log就很容易明白第一張圖了,第一種調用Service的方式也就簡單完成了;
第二種調用首先定義個繼承自ServiceConnection的BookConnection:
class BookServiceConnection implements ServiceConnection{public BookServiceConnection() {super();// TODO Auto-generated constructor stub}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub} }
第三個按鈕觸發的代碼:
Intent binderStartIntent=new Intent(this,BookService.class); connection=new BookServiceConnection();bindService(binderStartIntent, connection,Context.BIND_AUTO_CREATE);
第四種按鈕觸發的事件:
unbindService(connection);
看下Log對比之後也就明白了第二張圖:
這兩種都很簡單,不過有的時候Activity和Service之間是要通訊的:
這個時候你可以在BookService中定義一個內部類:
class BookBinder extends Binder {public BookService getCurrentService() {return BookService.this;}}
這個時候在Activity中的BookServiceConnection中onServicedConnected中改動一下:
@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub//擷取執行個體BookService bookService=((BookService.BookBinder)service).getCurrentService();//just do wo you want to do}
Service一直在後台工作,可以通過Notification將訊息傳遞到前台,修改一下BookService的onCreate()方法:
@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate(); NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("QQ空間") .setContentText("北京真坑,開個會房子都被拆"); mNotificationManager.notify(0, mBuilder.build());Log.i(tag, "開始onCreate啟動了");}
通知欄如下:
各位晚安,好夢~
Android組件之Service淺談