Android开发之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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.yusian.service;
 
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btnStartService, btnStopService, btnBindService, btnUnBindService;
    private MyService.MyBinder mBinder;
    private ServiceConnection connection;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btnStartService = (Button)findViewById(R.id.btn_start_service);
        btnStopService = (Button) findViewById(R.id.btn_close_service);
        btnBindService = (Button) findViewById(R.id.btn_bind_service);
        btnUnBindService = (Button) findViewById(R.id.btn_unbind_service);
        btnStartService.setOnClickListener(this);
        btnStopService.setOnClickListener(this);
        btnBindService.setOnClickListener(this);
        btnUnBindService.setOnClickListener(this);
 
        connection = new ServiceConnection() {
            @Override
            // Service与Activity建立连接
            public void onServiceConnected(ComponentName name, IBinder service) {
                // 这里的IBinder就是MyService中onBind()返回的IBinder
                mBinder = (MyService.MyBinder) service;
            }
            @Override
            // Service与Activity失去连接时调用
            public void onServiceDisconnected(ComponentName name) {
                mBinder = null;
            }
        };
    }
 
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MyService.class);
        switch (v.getId()){
            // 开启服务
            case R.id.btn_start_service:{
                startService(intent);
            }break;
            // 关闭服务
            case R.id.btn_close_service:{
                stopService(intent);
            }break;
            // 绑定服务
            case R.id.btn_bind_service:{
                bindService(intent, connection, BIND_AUTO_CREATE);
            }break;
            // 解绑服务
            case R.id.btn_unbind_service:{
                unbindService(connection);
            }break;
            default:break;
        }
    }
}
 
class MyService extends Service {
    private final static String TAG = "SALog";
    private MyBinder mBinder;
    public MyService() {
 
    }
    @Override
    public void onCreate() {
        super.onCreate();
        mBinder = new MyBinder();
        Log.d(TAG, "onCreate: ");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return mBinder;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
 
    class MyBinder extends Binder{
 
    }
}

Leave a Reply