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 }

 



posted @ 2017-04-19 22:43  张兮兮  阅读(176)  评论(0编辑  收藏  举报