普通Service和绑定Service

需要先在Manifest文件中对MyService进行声明:

<service android:name=".MyService" android:enabled="true"></service>

Service代码:

 1 package com.example.dbwater.myapplication;
 2 
 3 import android.content.ComponentName;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.content.ServiceConnection;
 7 import android.os.IBinder;
 8 import android.support.v7.app.AppCompatActivity;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.Toast;
12 
13 public class TestService extends AppCompatActivity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_test_service);
19     }
20     //普通方式启动
21     public void bt1_onClick(View v)
22     {
23         //准备Intent:显式意图
24         Intent intent = new Intent(this,MyService.class);
25         intent.putExtra("test","发送的数据");
26         //启动Service
27         startService(intent);
28         Toast.makeText(TestService.this, "Service已启动", Toast.LENGTH_SHORT).show();
29     }
30     //普通方式关闭
31     public void bt2_onClick(View v)
32     {
33         //准备Intent:显式意图
34         Intent intent = new Intent(this,MyService.class);
35         //关闭Service
36         stopService(intent);
37         Toast.makeText(TestService.this, "Service已停止", Toast.LENGTH_SHORT).show();
38     }
39     ServiceConnection sc;
40     MyService.MyBinder myb;
41     //绑定
42     public void bt3_onClick(View v)
43     {
44         //以绑定方式启动
45         //准备Intent:显式意图
46         Intent intent = new Intent(this,MyService.class);
47         if (sc==null) {
48             sc = new ServiceConnection() {
49                 @Override
50                 public void onServiceConnected(ComponentName name, IBinder service) {
51                     myb = (MyService.MyBinder) service;
52                     Toast.makeText(TestService.this, "绑定启动完成,接收返回的对象" + myb, Toast.LENGTH_SHORT).show();
53                 }
54 
55                 @Override
56                 public void onServiceDisconnected(ComponentName name) {
57                     Toast.makeText(TestService.this, "服务连接中断", Toast.LENGTH_SHORT).show();
58                 }
59             };
60         }
61         //三个参数
62         //1-意图
63         //2-服务连接的实现类
64         //3-启动方式,一般用Context.BIND_AUTO_CREATE
65         bindService(intent, sc, Context.BIND_AUTO_CREATE);
66     }
67     //解除绑定
68     public void bt4_onClick(View v)
69     {
70 
71         if (sc!=null) {
72             unbindService(sc);
73             sc=null;
74         }
75         else
76         {
77             Toast.makeText(TestService.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
78         }
79     }
80 
81 }
View Code

MyService代码:

 1 package com.example.dbwater.myapplication;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 import android.util.Log;
 8 
 9 /**
10  * Created by Administrator on 2016/8/9.
11  */
12 public class MyService extends Service {
13     public MyService() {
14         Log.e("TAG","MyService被创建");
15     }
16 
17     @Override
18     public void onCreate() {
19         Log.e("TAG","onCreate被调用");
20         super.onCreate();
21     }
22 
23     @Override
24     public void onDestroy() {
25         Log.e("TAG","onDestroy被调用");
26         super.onDestroy();
27     }
28 
29     @Override
30     public void onRebind(Intent intent) {
31         Log.e("TAG","onRebind被调用");
32         super.onRebind(intent);
33     }
34 
35     @Override
36     public boolean onUnbind(Intent intent) {
37         Log.e("TAG","onUnbind被调用");
38         return super.onUnbind(intent);
39     }
40 
41     @Override
42     public int onStartCommand(Intent intent, int flags, int startId) {
43         String string = intent.getStringExtra("test");
44         Log.e("TAG","onStartCommand被调用,并收到数据="+string);
45         return super.onStartCommand(intent, flags, startId);
46     }
47 
48     public class MyBinder extends Binder
49     {
50         //定义数据交换的方法
51 
52     }
53 
54     //回调方法
55     //绑定
56     @Override
57     public IBinder onBind(Intent intent) {
58         Log.e("TAG","onBind被调用");
59         // TODO: Return the communication channel to the service.
60 
61         //throw new UnsupportedOperationException("Not yet implemented");
62         return new MyBinder();
63     }
64 }
View Code

layout代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     android:orientation="vertical"
11     tools:context="com.example.dbwater.myapplication.TextService">
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="普通方式启动"
17         android:onClick="bt1_onClick"/>
18     <Button
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:text="普通方式停止"
22         android:onClick="bt2_onClick"/>
23     <Button
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content"
26         android:text="绑定方式启动"
27         android:onClick="bt3_onClick"/>
28     <Button
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content"
31         android:text="解绑方式停止"
32         android:onClick="bt4_onClick"/>
33 </LinearLayout>
View Code

 


posted on 2016-08-09 17:35  beens  阅读(304)  评论(0编辑  收藏  举报

导航