Android组件之Service浅谈

Service是Android中的四大组件之一,和windows中的服务是类似,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序Service,手机中有的程序的更新,服务的推送。Android系统中,Service与Activity类似,都需要AndroidManifest.xml文件中配置,而且生命周期有点类似。Service不像Activity在前台运行,而是默默的在后台默默的工作,简单粗暴点理解就是男主内女主外,分工明确。

Service生命周期

service与activity一样都存在与当前进程的主线程中,所以,一些阻塞UI的操作,比如耗时操作不能放在service里进行,比如另外开启一个线程来处理类似下载这种耗时操作。如果在service里进行一些耗CPU和耗时操作,应用会弹出是强制关闭还是等待的对话框。因此service和activity是平级的,四大组件之一的地位也不是浪得虚名。先看张经典老图:

Android组件之Service浅谈

 

这两个都是Service的生命周期,从上到到下也没有什么难懂的英文,应该比较好理解,如果有困惑,可以先参考下面的程序就懂了.

两种启动Service方式

自定义一个BookService继承自Service:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class BookService extends Service  {
 
    private String tag="BookService";
 
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.i(tag, "开始onCreate启动了");
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.i(tag, "开始执行onStartCommand启动了");
        Toast.makeText(this"BookService开始了",Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
         
    }
 
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Log.i(tag, "销毁onDestroy启动了");
        super.onDestroy();
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(tag, "绑定onBind启动了");
        return null;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(tag, "解绑onUnbind启动了");
        return super.onUnbind(intent);
    }
 
}

 

  看下效果图:

Android组件之Service浅谈

 

布局很简单,就不贴代码了,但是需要在AndroidManifest.xml注册一下代码:

1
<service android:name="com.example.googleservice.BookService"></service>

点击第一个按钮执行的代码,Intent之前有写过,之前是startActivity,这里用的startService:

 

1
2
Intent service=new Intent(this,BookService.class);
        startService(service);

 

  第二个按钮执行的事件:

1
2
Intent stopservice=new Intent(this,BookService.class);
            stopService(stopservice);

  通过Log就很容易明白第一张图了,第一种调用Service的方式也就简单完成了;

Android组件之Service浅谈

 

第二种调用首先定义个继承自ServiceConnection的BookConnection:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class BookServiceConnection implements ServiceConnection{
 
     public BookServiceConnection() {
         super();
         // TODO Auto-generated constructor stub
     }
 
     @Override
     public void onServiceConnected(ComponentName name, IBinder service) {
         // TODO Auto-generated method stub
     }
 
     @Override
     public void onServiceDisconnected(ComponentName name) {
         // TODO Auto-generated method stub
          
     }
      
 }

 第三个按钮触发的代码:

1
2
3
Intent binderStartIntent=new Intent(this,BookService.class);
             connection=new BookServiceConnection();
            bindService(binderStartIntent, connection,Context.BIND_AUTO_CREATE);

  第四种按钮触发的事件:

1
unbindService(connection);

  看下Log对比之后也就明白了第二张图:

Android组件之Service浅谈

这两种都很简单,不过有的时候Activity和Service之间是要通信的:

这个时候你可以在BookService中定义一个内部类:

 

1
2
3
4
5
class BookBinder extends Binder {
    public BookService getCurrentService() {
        return BookService.this;
    }
}

 

 这个时候在Activity中的BookServiceConnection中onServicedConnected中改动一下:

1
2
3
4
5
6
7
@Override
    public 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()方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.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浅谈

 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4077587.html,如需转载请自行联系原作者

上一篇:hdu3635 Dragon Balls(带权并查集)


下一篇:同学,你有一份阿里前端终面官分享的校招秘籍待查收