android的service本地服务的理解

本文详细解析了Android本地服务的实现原理,包括Acitivity、Service、Binder和ServiceConnection的作用,以及服务绑定与解除绑定的过程。并通过代码示例展示了如何在Android应用中使用本地服务。

这两天在学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() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			Toast.makeText(getApplicationContext(), "--Service DisConnected--",
					Toast.LENGTH_LONG).show();

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			Toast.makeText(getApplicationContext(), "--Service Connected--",
					Toast.LENGTH_LONG).show();
			binder = (BindService.MyBinder) service;
		}
	};

	@Override
	protected 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() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bindService(intent, conn, Service.BIND_AUTO_CREATE);
			}
		});

		unbind.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				unbindService(conn);
			}
		});

		getServiceStatus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),
						"Service的count的值为:" + binder.getCount(), 4000).show();
			}
		});
	}

	@Override
	public 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;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(getApplicationContext(), "Service is Binded",
				Toast.LENGTH_LONG).show();
		return binder;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.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();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(getApplicationContext(), "Service is Unbinded",
				Toast.LENGTH_LONG).show();
		return true;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.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>

嗯,就是这样。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值