安卓:绑定服务的一个小应用

如图:


注意:清单文件里注册服务:<receiver  android:name="包名+类名"></receiver>  


逻辑代码文件:

<span style="font-size:18px;">package com.example.day22_service4;

import com.example.day22_service4.MyService.MyBind;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	TextView tv;
	MyConn conn=new MyConn();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById(R.id.tv);
    
    }

    public void click(View v)
    {
    	switch(v.getId())
    	{
    	case R.id.bt1:
    		Intent intent=new Intent(MainActivity.this,MyService.class);
    		bindService(intent, conn, Context.BIND_AUTO_CREATE);
    		
    		break;
    	case R.id.bt2:
    		unbindService(conn);
    		break;
    		
    		
    	}
    }
    class MyConn implements ServiceConnection
    {
    	/**
		 * 表示服务链接成功后回调的方法
		 * 
		 * 参数1:组件的名称
		 * 参数2:链接后onBind()返回的值
		 */
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			MyBind bi=(MyBind) service;
			MyService ms=bi.getService();
			int num=ms.getRandom();
			tv.setText("服务中返回的随机数:"+num);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			conn=null;
		}
    	
    }
}
</span>


继承service的类文件:

<span style="font-size:18px;">package com.example.day22_service4;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service{

	@Override
	public IBinder onBind(Intent intent) {

		return new MyBind();
	}
	
	@Override
	public boolean onUnbind(Intent intent) {

		return super.onUnbind(intent);
	}
	/*
	 * 如果想访问getRandom()方法,service对象不能直接去new
	 * 需要传递过去,通过IBinder向外暴露服务对象
	 */
	class MyBind extends Binder
	{
		//向外暴漏service对象
		public MyService getService()
		{
			return MyService.this;
		}
	}
	public int getRandom()
	{
		int num=(int)(Math.random()*10+1);
		return num;
	}
}
</span>


布局文件:

<span style="font-size:18px;"><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/bt1"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:text="绑定服务"
	    android:onClick="click"/>
	<Button 
	    android:id="@+id/bt2"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:text="解绑服务"
	    android:onClick="click"
	    android:layout_below="@+id/bt1"/>
    <TextView
        android:id="@+id/tv"
        android:layout_below="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ff00"/>

</RelativeLayout>
</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值