bindService与startService区别

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 }
时间: 2024-08-10 18:37:13

bindService与startService区别的相关文章

Android里Service的bindService()和startService()混合使用深入分析

先讲讲怎么使用bindService()绑定服务 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回一个用来与service交互的IBinder绑定是异步的.bindService()会立即返回,它不会返回IBinder给客户端.要接收IBinder,客户端必须创建一个ServiceConnection的实例并传给bindService().ServiceConnection包含一个回调方法,系统调

bindService和startService的区别

区别: startService,关闭服务退出activity,service仍然处于后台运行 bindService,关闭服务退出activity直接stopService,停止服务 bindService用于绑定一个服务,与服务进行通讯:startService用于启动停止服务 bindService方式的一般过程: ①新建Service类BindService.在BindService类里新建内部类MyBinder,继承自Binder(Binder实现IBinder接口).MyBinder

理解Android的startservice和bindservice(转)

一.首先,让我们确认下什么是service? service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比较高,它比处于前台的应用优先级低,但是比后台的其他应用优先级高,这就决定了当系统因为缺少内存而销毁某些没被利用的资源时,它被销毁的概率很小哦. 二.那么,什么时候,我们需要使用service呢?         我们知道,service是运行在后台的应用,对于用户来说失去了被关注的焦点.这就跟我们打开了音乐播放之后

深入理解Android的startservice和bindservice

一.首先,让我们确认下什么是service?         service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比较高, 它比处于前台的应用优先级低,但是比后台的其他应用优先级高,这就决定了当系统因为缺少内存而销毁某些没被利用的资源时,它被销毁的概率很小哦. 二.那么,什么时候,我们需要使用service呢?         我们知道,service是运行在后台的应用,对于用户来说失去了被关注的焦点.这就跟我们

Android后台进程与前台线程间的区别使用

博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 很早就翻译过Android API的一篇文章Android高级开发第四讲--API之Service,今天主要讲些实战的东西 比如Service的启动方式有两种,首先在AndroidManifest.xml中配置Service,然后通过bindService和startService来启动, 不同点在于: 执行方式:前者启动时执行onCreate-->onBin

Android-Service (基本知识,生命周期,实例-startService 启动的服务音乐播放器后台服务播放)

1.回顾 上篇 学习了 Android的四大组件之一 BroadCastReceiver 的 相关知识 2.重点 (1)Service 分类 (2)Service 的生命周期 (3)Service 标签 下的属性 (4)什么时候使用BindService 和 startService ? (5)实例 - 通过Service 服务 实现 音乐播放器 后台播放 3.Service 分类 3.1 按照地点分 (1)本地服务 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外Local

【转】深入理解Android的startservice和bindservice--不错

原文网址:http://www.cnblogs.com/yejiurui/p/3429451.html 一.首先,让我们确认下什么是service?         service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互.它必须由用户或者其他程序显式的启动.它的优先级比较高,它比处于前台的应用优先级低,但是比后台的其他应用优先级高,这就决定了当系统因为缺少内存而销毁某些没被利用的资源时,它被销毁的概率很小哦. 二.那么,什么时候,我们需要使用service呢? 

Service之常用方法

(一)StartService 运行Service的方法之一.任何继承于android.content.Context的Android组件(component)都可以使用一个Intent(android.content.Intent)来开启一个Service.Intent里面可以以类对象(Class<?>)或者action进行构造,使用action构造对于初学者来说不太直观,而且action构造的适用范围比类对象构造使用范围更广,所以action构造的方法将在本系列文章后面的内容进行介绍,现在

Android四大组件-Service

http://blog.csdn.net/guolin_blog/article/details/11952435 http://www.jianshu.com/p/eeb2bd59853f 概述 定义.特点: Service是可以在后台执行长时间(长生命周期)而又不与用户产生UI交互(没有用户界面)的操作 注意事项:1.只能在后台运行,即便用户切换了其他应用,启动的Service仍可在后台运行.2.可以和其他组件进行Service绑定并与之交互,甚至是跨进程通信(IPC).3.不能运行在一个独