Android(java)学习笔记174:服务(service)之混合方式开启服务

1. 前面我们已经讲过可以使用两种方式开启服务

startService----stopService:

       oncreate() ---> onstartCommand() ---> onstartCommand()---> onDestory();

bindService----unbindService:

       oncreate() ---> onbind() --->onUnbind() ---> onDestory();

 

为什么需要采用混合的方式开启服务?

答:startService服务长期后天运行,不可以调用服务里面的方式;

bindService可以调用服务的方法,但是不能长期在后台运行。(bindService和Activity同时死)

综合上面两种方式的优点,采用混合的方式开启服务(继承了上面两种的优点):

(1)服务长期后台运行

(2)可以调用服务的方法

 

2.利用案例说明混合方式开启服务:

(1)创建一个Android项目如下:

 

(2)我们首先看看布局文件activity_main.xml,如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:onClick="start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启服务" />
    <Button
        android:onClick="stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止服务" />
    <Button
        android:onClick="bind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绑定服务" />
    <Button
        android:onClick="unbind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="解除绑定服务" />
    <Button
        android:onClick="call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="调用服务的方法" />

</LinearLayout>

布局效果如下:

(3)其中MainActivity.java

 1 package com.itheima.mix;
 2 
 3 import android.app.Activity;
 4 import android.content.ComponentName;
 5 import android.content.Intent;
 6 import android.content.ServiceConnection;
 7 import android.os.Bundle;
 8 import android.os.IBinder;
 9 import android.view.View;
10 
11 public class MainActivity extends Activity {
12     private MyConn conn;
13     private IService iService;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19     }
20     //以start的方式i开启服务
21     public void start(View view){
22         Intent intent = new Intent(this,TestService.class);
23         startService(intent);
24     }
25     //停止服务
26     public void stop(View view){
27         Intent intent = new Intent(this,TestService.class);
28         stopService(intent);
29     }
30     //绑定服务u
31     public void bind(View view){
32         Intent intent = new Intent(this,TestService.class);
33         conn = new MyConn();
34         bindService(intent, conn, BIND_AUTO_CREATE);
35     }
36     private class MyConn implements ServiceConnection{
37         @Override
38         public void onServiceConnected(ComponentName name, IBinder service) {
39             iService = (IService) service;
40         }
41 
42         @Override
43         public void onServiceDisconnected(ComponentName name) {
44             
45         }
46         
47     }
48     //解绑服务
49     public void unbind(View view){
50         unbindService(conn);
51     }
52 
53     public void call (View view){
54         iService.callMethodInService();
55     }
56     
57 }

其中使用到的Service为自定义的TestService,如下:

 1 package com.itheima.mix;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 import android.widget.Toast;
 8 
 9 public class TestService extends Service {
10     
11     private class MyBinder extends Binder implements IService{
12         @Override
13         public void callMethodInService() {
14             methodInService();
15         }
16     }
17     
18 
19     @Override 
20     public IBinder onBind(Intent intent) {
21         System.out.println("服务被绑定 onBind");
22         return new MyBinder();
23     }
24     @Override
25     public boolean onUnbind(Intent intent) {
26         System.out.println("服务被解绑 onUnbind");
27         return super.onUnbind(intent);
28     }
29     
30     @Override
31     public void onCreate() {
32         System.out.println("服务被创建 oncreate");
33         super.onCreate();
34     }
35     @Override
36     public int onStartCommand(Intent intent, int flags, int startId) {
37         System.out.println("onStartCommand");
38         return super.onStartCommand(intent, flags, startId);
39     }
40     
41     @Override
42     public void onDestroy() {
43         System.out.println("服务被销毁 onDestroy");
44         super.onDestroy();
45     }
46 
47     public void methodInService(){
48         Toast.makeText(this, "我是服务里面的方法", 0).show();
49     }
50 }

还有上面使用到的接口IService,如下:

package com.itheima.mix;

public interface IService {
    public void callMethodInService();
}

 

既然上面使用到了自定义的service的TestService,所以要在AndroidMainfest.xml注册TestService这个组件,如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.itheima.mix"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.itheima.mix.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <service android:name="com.itheima.mix.TestService"></service>
26     </application>
27 
28 </manifest>

 

3. 特别注意:

混合模式开启服务  

推荐使用的步骤

                   (1)startService()        ----> 保证服务长期后天运行

                   (2)如果要调用服务的方法  ---->bindService()绑定服务

                   (3)就可以调用服务的方法

                   (4)unbindservice    解绑服务

                   (5)服务还是长期后台运行

                   (6)如果要停止服务,stopService();

posted on 2015-09-11 16:49  鸿钧老祖  阅读(223)  评论(0编辑  收藏  举报

导航