android的service本地服务的理解

来源:互联网 发布:ubuntu 移除软件 编辑:程序博客网 时间:2024/06/07 12:08

这两天在学android的service,感觉老师上课没讲清楚,就下课自己补习下了。

本地服务的service,通过绑定实现。

其中主要涉及到Acitvity、Service、Binder和ServiceConnection这几个组件。

Acitivity就不用说了,就是要绑定service的程序;Service也就呵呵了。

Binder根据谷歌的翻译是“沾合剂”,感觉不是很形象,可以理解为在Service端的内部类,当Activity绑定这个Service后,可将这个内部Binder给提取出来,这样Activity就可以调用Service 的方法,让service一直工作了。

ServiceConnection就是Activity和Service之间的通道,通过这个通道来维持Activity和指定的Service进行绑定。

在我理解的本地服务绑定流程:

1.Activity通过BindService(Intent,ServiceConnection,int)请求道Intent 指定的service;

2.通过intent找到对应的Service,通过int的值来判断创建或打开Service的操作,比如Service.BIND_AUTO_CREATE的意思就是如果没有创建Service,创建后打开,如果已存在,直接打开。

3.Service调用OnCreate()创建Service;

4.Service通过OnBind(Intent )进行绑定,返回在Service中的内部类Binder;

5.ServiceConnection创建连接,并把Service中的Binder赋值给Activity中的Binder;

6.Activity通过调用Binder 的方法来获得值。


本地服务解除绑定:

1.Activity调用unbindSerivce(ServiceConnection),提出分手请求;

2.Service调用onUnbind(Intent)  返回true;

3.Service嗲用onDestroy()方法结束Service。


感觉这样理解就清晰一些。


附上代码:

MainActivity.java

package com.example.bindservice;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {Button bind, unbind, getServiceStatus;BindService.MyBinder binder;private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "--Service DisConnected--",Toast.LENGTH_LONG).show();}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "--Service Connected--",Toast.LENGTH_LONG).show();binder = (BindService.MyBinder) service;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bind = (Button) findViewById(R.id.button1);unbind = (Button) findViewById(R.id.button2);getServiceStatus = (Button) findViewById(R.id.button3);final Intent intent = new Intent();intent.setAction("service.BIND_SERVICE");bind.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubbindService(intent, conn, Service.BIND_AUTO_CREATE);}});unbind.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubunbindService(conn);}});getServiceStatus.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(),"Service的count的值为:" + binder.getCount(), 4000).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

BindService.java

package com.example.bindservice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.widget.Toast;public class BindService extends Service {private int count;private boolean quit;private MyBinder binder = new MyBinder();public class MyBinder extends Binder {public int getCount() {return count;}}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "Service is Binded",Toast.LENGTH_LONG).show();return binder;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();Toast.makeText(getApplicationContext(), "Service is Created",Toast.LENGTH_LONG).show();new Thread() {public void run() {while (!quit) {try {Thread.sleep(1000);} catch (InterruptedException e) {}count++;}};}.start();}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "Service is Unbinded",Toast.LENGTH_LONG).show();return true;}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();this.quit = true;Toast.makeText(getApplicationContext(), "Service is Destroyed",Toast.LENGTH_LONG).show();}}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.bindservice"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.bindservice.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".BindService" >            <intent-filter>                <action android:name="service.BIND_SERVICE" />            </intent-filter>        </service>    </application></manifest>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="绑定service" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button1"        android:layout_alignBottom="@+id/button1"        android:layout_toRightOf="@+id/button1"        android:text="解除绑定" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button2"        android:layout_alignBottom="@+id/button2"        android:layout_toRightOf="@+id/button2"        android:text="获取srevice状态" /></RelativeLayout>

嗯,就是这样。



0 0