Android攻城狮四大组件之Service
生活随笔
收集整理的這篇文章主要介紹了
Android攻城狮四大组件之Service
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
組件篇——Service
當Android系統內存不足的時候,會殺死優先級別較低的Activity,而基本上Service的優先級要高于Activity,所以程序中如果含有 Service,那么該程序是很難被殺掉的,而且即使 Service 被殺掉,它也很容易自己再次啟動,除非人為去停掉。
-------------------
Service 本身不能做耗時操作(因為它運行在主線程)。耗時操作交給 Handle 或者 AsyncTask 去處理。 1. 本地服務(Local Service)
- 應用程序內部
- startService(啟動服務)、stopService(在“啟動源”或者 Activity 中停止服務)、stopSelf(在Service停止服務)、stopSelfResult(在Service中停止服務),后面兩種都是服務的自我停止。
- bindService(綁定服務)、unbindService(解綁)
通過 startService 或者 bindService 都可以啟動服務。2. 遠程服務(Remote Service)
- Android系統內部的應用程序之間(不是手機之間)
- 定義 IBinder 接口,通過它,把數據暴露出來,然后把數據提供給啟動源或者其他程序。
遠程服務只能通過 IBinder 去啟動服務。
--------------
我們知道一個Activity是有生命周期的,并且必須在配置文檔中進行注冊。而Service和Activity是類似的,有類似的生命周期,也必須注冊。
繼承關系:
Service 繼承 ContextWrapper,Activity 繼承 ContextThemeWrapper,二者共同擁有一個“祖宗類”——Context。
------------------
如圖所示是 Service 的兩種生命周期(分別以startService和bindService啟動)。
Start方式特點:- 服務跟啟動源沒有任何聯系- 無法得到服務對象
Bind方式特點:- 通過 Ibinder 接口實例,返回一個ServiceConnection對象給啟動源- 通過 ServiceConnection對象的相關方法可以得到Service對象
BindService: 支持數據回傳。 什么時候使用Start?當服務不需要和啟動源有任何關聯的時候。這樣有可能造成的結果:程序都退出了,但Service依然存在,為什么呢?因為二者沒有關系。 不過,利用這種特性,也可以做一些特殊的應用,比如:UI不要了,但后臺操作仍然繼續的例子。但比較麻煩是就是:數據沒辦法回傳了。 所以,既想讓它們(服務和啟動源)分離,又想得到回傳數據,就需要將 Start 和 Bind 混合使用。
1 public class MainActivity extends ActionBarActivity { 2 Intent intent; 3 Intent intent2; 4 MyBindService service; 5 ServiceConnection conn=new ServiceConnection() { 6 //當啟動源跟service的連接意外丟失會調用這個方法 7 //比如當service崩潰了或者被強行殺死了 8 @Override 9 public void onServiceDisconnected(ComponentName name) { 10 // TODO Auto-generated method stub 11 12 } 13 //當啟動源跟service的連接后會自動調用 14 @Override 15 public void onServiceConnected(ComponentName name, IBinder binder) { 16 // TODO Auto-generated method stub 17 service=((MyBinder)binder).getservice(); 18 } 19 }; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.fragment_main); 24 25 } 26 27 public void click(View view) { 28 switch (view.getId()) { 29 case R.id.start: 30 intent = new Intent(MainActivity.this, MyStartService.class); 31 startService(intent); 32 break; 33 34 case R.id.stop: 35 stopService(intent); 36 break; 37 38 case R.id.paly: 39 service.Play(); 40 break; 41 case R.id.pervious: 42 service.Pervious(); 43 break; 44 case R.id.next: 45 service.next(); 46 break; 47 case R.id.paus: 48 service.Pause(); 49 break; 50 case R.id.bind: 51 intent2 = new Intent(MainActivity.this, MyBindService.class); 52 //startService(intent2); 53 bindService(intent2, conn, Service.BIND_AUTO_CREATE); 54 55 break; 56 57 case R.id.unbind: 58 unbindService(conn); 59 break; 60 61 } 62 } 63 // @Override 64 // protected void onDestroy() { 65 // // TODO Auto-generated method stub 66 // stopService(intent2); 67 // unbindService(conn); 68 // super.onDestroy(); 69 // } 70 71 } 1 public class MyBindService extends Service { 2 @Override 3 public void onCreate() { 4 // TODO Auto-generated method stub 5 super.onCreate(); 6 Log.i("--->>", "BindService--onCreate()"); 7 } 8 9 @Override 10 public boolean onUnbind(Intent intent) { 11 // TODO Auto-generated method stub 12 Log.i("--->>", "BindService--onUnbind()"); 13 return super.onUnbind(intent); 14 } 15 16 @Override 17 public void onDestroy() { 18 // TODO Auto-generated method stub 19 super.onDestroy(); 20 Log.i("--->>", "BindService--onDestroy()"); 21 } 22 23 public class MyBinder extends Binder { 24 public MyBindService getservice() { 25 26 return MyBindService.this; 27 28 } 29 } 30 31 @Override 32 public IBinder onBind(Intent intent) { 33 // TODO Auto-generated method stub 34 Log.i("--->>", "BindService--onBind()"); 35 36 return new MyBinder(); 37 } 38 39 public void Play() { 40 Log.i("--->>", "播放"); 41 } 42 public void Pause() { 43 Log.i("--->>", "暫停"); 44 } 45 public void Pervious() { 46 Log.i("--->>", "上一首"); 47 } 48 public void next() { 49 Log.i("--->>", "下一首"); 50 } 51 }
BindService: 支持數據回傳。 什么時候使用Start?當服務不需要和啟動源有任何關聯的時候。這樣有可能造成的結果:程序都退出了,但Service依然存在,為什么呢?因為二者沒有關系。 不過,利用這種特性,也可以做一些特殊的應用,比如:UI不要了,但后臺操作仍然繼續的例子。但比較麻煩是就是:數據沒辦法回傳了。 所以,既想讓它們(服務和啟動源)分離,又想得到回傳數據,就需要將 Start 和 Bind 混合使用。
1 public class MainActivity extends ActionBarActivity { 2 Intent intent; 3 Intent intent2; 4 MyBindService service; 5 ServiceConnection conn=new ServiceConnection() { 6 //當啟動源跟service的連接意外丟失會調用這個方法 7 //比如當service崩潰了或者被強行殺死了 8 @Override 9 public void onServiceDisconnected(ComponentName name) { 10 // TODO Auto-generated method stub 11 12 } 13 //當啟動源跟service的連接后會自動調用 14 @Override 15 public void onServiceConnected(ComponentName name, IBinder binder) { 16 // TODO Auto-generated method stub 17 service=((MyBinder)binder).getservice(); 18 } 19 }; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.fragment_main); 24 25 } 26 27 public void click(View view) { 28 switch (view.getId()) { 29 case R.id.start: 30 intent = new Intent(MainActivity.this, MyStartService.class); 31 startService(intent); 32 break; 33 34 case R.id.stop: 35 stopService(intent); 36 break; 37 38 case R.id.paly: 39 service.Play(); 40 break; 41 case R.id.pervious: 42 service.Pervious(); 43 break; 44 case R.id.next: 45 service.next(); 46 break; 47 case R.id.paus: 48 service.Pause(); 49 break; 50 case R.id.bind: 51 intent2 = new Intent(MainActivity.this, MyBindService.class); 52 //startService(intent2); 53 bindService(intent2, conn, Service.BIND_AUTO_CREATE); 54 55 break; 56 57 case R.id.unbind: 58 unbindService(conn); 59 break; 60 61 } 62 } 63 // @Override 64 // protected void onDestroy() { 65 // // TODO Auto-generated method stub 66 // stopService(intent2); 67 // unbindService(conn); 68 // super.onDestroy(); 69 // } 70 71 } 1 public class MyBindService extends Service { 2 @Override 3 public void onCreate() { 4 // TODO Auto-generated method stub 5 super.onCreate(); 6 Log.i("--->>", "BindService--onCreate()"); 7 } 8 9 @Override 10 public boolean onUnbind(Intent intent) { 11 // TODO Auto-generated method stub 12 Log.i("--->>", "BindService--onUnbind()"); 13 return super.onUnbind(intent); 14 } 15 16 @Override 17 public void onDestroy() { 18 // TODO Auto-generated method stub 19 super.onDestroy(); 20 Log.i("--->>", "BindService--onDestroy()"); 21 } 22 23 public class MyBinder extends Binder { 24 public MyBindService getservice() { 25 26 return MyBindService.this; 27 28 } 29 } 30 31 @Override 32 public IBinder onBind(Intent intent) { 33 // TODO Auto-generated method stub 34 Log.i("--->>", "BindService--onBind()"); 35 36 return new MyBinder(); 37 } 38 39 public void Play() { 40 Log.i("--->>", "播放"); 41 } 42 public void Pause() { 43 Log.i("--->>", "暫停"); 44 } 45 public void Pervious() { 46 Log.i("--->>", "上一首"); 47 } 48 public void next() { 49 Log.i("--->>", "下一首"); 50 } 51 }
?
轉載于:https://www.cnblogs.com/my334420/p/6736337.html
總結
以上是生活随笔為你收集整理的Android攻城狮四大组件之Service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: postman接口自动化(三)变量设置与
- 下一篇: cmd默认是以管理员身份运行