android api分析22 Service

本文详细介绍了在Android应用中如何使用Service,并通过代码示例展示了Service的创建、绑定、解绑以及获取Service状态的过程。文章还讨论了Service的生命周期管理和线程操作,帮助开发者更好地理解和掌握Service的使用技巧。
public class MainActivity extends Activity {
	
	Button bind, unbind, getServiceStatus;
	// 保持所启动的Service的IBinder对象
	BindService.MyBinder binder;
	// 定义一个ServiceConnection对象
	
	private ServiceConnection conn = new ServiceConnection() {
		// 当该Activity与Service连接成功时回调该方法
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.e("service02", "MainActivity onServiceConnected");
			// 获取Service的onBind方法所返回的MyBinder对象
			binder = (BindService.MyBinder) service;
		}

		// 当该Activity与Service断开连接时回调该方法
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.e("service02", "MainActivity onServiceDisconnected");
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.e("service02", "MainActivity onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获取程序界面中的start、stop、getServiceStatus按钮
		bind = (Button) findViewById(R.id.bind);
		unbind = (Button) findViewById(R.id.unbind);
		getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
		// 创建启动Service的Intent
		final Intent intent = new Intent();
		// 为Intent设置Action属性
		intent.setAction("org.crazyit.service.BIND_SERVICE");
		bind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View source) {
				// 绑定指定Serivce
				bindService(intent, conn, Service.BIND_AUTO_CREATE);
			}
		});
		unbind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View source) {
				// 解除绑定Serivce
					unbindService(conn);
			}
		});
		getServiceStatus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View source) {
				// 获取、并显示Service的count值
				Toast.makeText(MainActivity.this,
						"Serivce的count值为:" + binder.getCount(), 4000).show();
			}
		});
	}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/bind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bind" />

    <Button
        android:id="@+id/unbind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unbind" />

    <Button
        android:id="@+id/getServiceStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="getServiceStatus" />

</LinearLayout>

public class BindService extends Service
{
	private int count;
	private boolean quit;
	// 定义onBinder方法所返回的对象
	private MyBinder binder = new MyBinder();
	// 通过继承Binder来实现IBinder类
	public class MyBinder extends Binder
	{
		public int getCount()
		{
			Log.e("service02", "BindService getCount");
			// 获取Service的运行状态:count
			return count;
		}
	}
	// 必须实现的方法
	@Override
	public IBinder onBind(Intent intent)
	{
		Log.e("service02", "BindService onBind");
		// 返回IBinder对象
		return binder;
	}
	// Service被创建时回调该方法。
	@Override
	public void onCreate()
	{
		super.onCreate();
		Log.e("service02", "BindService onCreate");
		// 启动一条线程、动态地修改count状态值
		new Thread()
		{
			@Override
			public void run()
			{
				while (!quit)
				{
					try
					{
						Thread.sleep(1000);
					}
					catch (InterruptedException e)
					{
					}
					count++;
				}
			}
		}.start();		
	}
	// Service被断开连接时回调该方法
	@Override
	public boolean onUnbind(Intent intent)
	{
		Log.e("service02", "BindService onUnbind");
		return true;
	}
	// Service被关闭之前回调。
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		this.quit = true;
		Log.e("service02", "BindService onDestroy");
	}
	
	@Override
	public void onRebind(Intent intent) 
	{
		super.onRebind(intent);
		this.quit = true;
		Log.e("service02", "BindService onRebind");
	}	
}

<service android:name=".BindService">
			<intent-filter>
				<!-- 为该Service组件的intent-filter配置action -->
				<action android:name="org.crazyit.service.BIND_SERVICE" />
			</intent-filter>	
		</service>

ServiceConnection

监视服务的状态


Binder extends Object implements IBinder



点击bind
BindService onCreate()
BindService onBind()
MainActivity onServiceConnected()


之后 count在新线程中 值会一直增加


点击unbind
BindService onUnbind()
BindService onDestroy()
count值停止增加


点击getServiceStatus
BindService getCount()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值