码迷,mamicode.com
首页 > 其他好文 > 详细

bindService与startService区别

时间:2015-08-28 17:32:14      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

1. Started Service中使用startService()方法来进行方法的调用,调用者和服务之间没有联系,即使调用者退出了,服务依然在进行 【onCreate()-  >onStartCommand()->startService()->onDestroy()】,注意其中没有 onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。
2. BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()->onBind()->onUnbind()->onDestroy()】。

MainActivity

  1 package com.stone.test2;
  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.ActionBarActivity;
  9 import android.os.Bundle;
 10 import android.view.Menu;
 11 import android.view.MenuItem;
 12 import android.view.View;
 13 import android.widget.EditText;
 14 import android.widget.TextView;
 15 
 16 
 17 public class MainActivity extends ActionBarActivity implements View.OnClickListener, ServiceConnection {
 18 
 19     private EditText edittext;
 20     private TextView textview;
 21     private MyService.MyBinder myBinder;
 22 
 23     @Override
 24     protected void onCreate(Bundle savedInstanceState) {
 25         super.onCreate(savedInstanceState);
 26         setContentView(R.layout.activity_main);
 27         edittext= (EditText) findViewById(R.id.edittext);
 28         textview= (TextView) findViewById(R.id.textview);
 29         System.out.println("onCreate");
 30         findViewById(R.id.btnStart).setOnClickListener(this);
 31         findViewById(R.id.btnStop).setOnClickListener(this);
 32         findViewById(R.id.btnBindService).setOnClickListener(this);
 33         findViewById(R.id.btnUnbindService).setOnClickListener(this);
 34         findViewById(R.id.btnSyn).setOnClickListener(this);
 35 
 36     }
 37 
 38     @Override
 39     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 40         super.onActivityResult(requestCode, resultCode, data);
 41         textview.setText(data.getStringExtra("data"));
 42     }
 43 
 44     @Override
 45     public boolean onCreateOptionsMenu(Menu menu) {
 46         // Inflate the menu; this adds items to the action bar if it is present.
 47         getMenuInflater().inflate(R.menu.menu_main, menu);
 48         return true;
 49 
 50     }
 51 
 52     @Override
 53     public boolean onOptionsItemSelected(MenuItem item) {
 54         // Handle action bar item clicks here. The action bar will
 55         // automatically handle clicks on the Home/Up button, so long
 56         // as you specify a parent activity in AndroidManifest.xml.
 57         int id = item.getItemId();
 58 
 59         //noinspection SimplifiableIfStatement
 60         if (id == R.id.action_settings) {
 61             return true;
 62         }
 63 
 64         return super.onOptionsItemSelected(item);
 65     }
 66 
 67     @Override
 68     public void onClick(View v) {
 69         switch (v.getId()) {
 70             case R.id.btnStart:
 71 //                System.out.println("who are you");
 72                 Intent intent = new Intent(this,MyService.class);
 73                 intent.putExtra("data",edittext.getText().toString());
 74                 startService(intent);
 75                 break;
 76             case R.id.btnStop:
 77                 stopService(new Intent(this, MyService.class));
 78                 break;
 79             case R.id.btnBindService:    //绑定服务,通过实现ServiceConnection接口
 80                 bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE);
 81                 break;
 82             case R.id.btnUnbindService:  //解除绑定服务
 83                 unbindService(this);
 84                 break;
 85             case R.id.btnSyn:            //同步数据
 86                 if (myBinder!=null){
 87                     myBinder.setData(edittext.getText().toString());
 88                 }
 89                 break;
 90         }
 91     }
 92 
 93     @Override
 94     public void onServiceConnected(ComponentName name, IBinder service) {
 95         myBinder = (MyService.MyBinder) service;   //同步数据
 96     }
 97 
 98     @Override
 99     public void onServiceDisconnected(ComponentName name) {
100 
101     }
102 }
 1 package com.stone.test2;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 
 8 public class MyService extends Service {
 9     private String data;
10     private boolean running;
11 
12     public MyService() {
13     }
14 
15     @Override
16     public IBinder onBind(Intent intent) {
17         return new MyBinder();
18     }
19 
20     public class MyBinder extends Binder{   //与MainActivity通信
21 
22         public void setData(String data){
23             MyService.this.data=data;
24         }
25     }
26 
27     @Override
28     public int onStartCommand(Intent intent, int flags, int startId) {
29         data = intent.getStringExtra("data");
30         return super.onStartCommand(intent, flags, startId);
31     }
32 
33     @Override
34     public void onCreate() {
35         super.onCreate();
36         running = true;
37         new Thread(){
38             @Override
39             public void run() {
40                 super.run();
41                 System.out.println("fucking");
42                 while (running) {
43                     System.out.println("正在读取:" + data);
44                     try {
45                         sleep(1000);
46                     } catch (InterruptedException e) {
47                         e.printStackTrace();
48                     }
49                 }
50             }
51         }.start();
52 
53     }
54 
55     @Override
56     public void onDestroy() {
57         super.onDestroy();
58         running = false;
59     }
60 }

 

bindService与startService区别

标签:

原文地址:http://www.cnblogs.com/yesphet/p/4766786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!