android开发学习笔记——Bound Service

经过进一步学习发现昨天的程序(下面的程序)存在问题

		bindService(intent, conn, BIND_AUTO_CREATE);
		System.out.println(servicedemo.i+" dsds");
     之后返回的值并不是ServiceConnection 执行之后的结果,其实通过system.out输出可以发现,程序是先执行的System.out.println(servicedemo.i+" dsds");后执行ServiceConnection当中的servicedemo = ((ServiceDemo.LocalBinder) service).getService(); 可以仿照developer 文档当中的bound Service举例进行正确设置。

1. bindService(intent, conn, BIND_AUTO_CREATE);  实现绑定    绑定之后自动调用

public void onServiceConnected(ComponentName name, IBinder service) 

2.IBinder 为绑定成功后,public IBinder onBind(Intent intent) 的返回值,使用该对象作为activity和service的接口。

3.同时,绑定成功之后会调用service当中的oncreate(),onbind()方法

Activity 

package com.example.servicebinddemo;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.*;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	ServiceDemo servicedemo = new ServiceDemo();
	// 创建connection对象
	MyServiceConnection conn = new MyServiceConnection();

	// 绑定service
	public void startservice(View view) {
		Intent intent = new Intent(MainActivity.this, ServiceDemo.class);
		// 绑定 当绑定成功之后执行connection当中的onServiceConnected()方法
		bindService(intent, conn, BIND_AUTO_CREATE);
		System.out.println(servicedemo.i+" dsds");
	}

	// 解除绑定不会立即执行connection当中的onServicedisconnected
	public void stopservice(View view) {
		unbindService(conn);
		super.onDestroy();
		System.out.println("destroy");
	}

	// implement service connection 主要是复写其中的onserviceconnected
	// 和onserviceDisconnected方法
	class MyServiceConnection implements ServiceConnection {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			//参数IBinder service为service当中onbind()返回的ibinder
			servicedemo = ((ServiceDemo.LocalBinder) service).getService();
			Toast toast = Toast.makeText(MainActivity.this, "connection",
					Toast.LENGTH_LONG);
			toast.show();
			System.out.println("onServiceConnected");
		}

		@Override
		//service 被停止或杀死之后才会执行,不是unbindService之后就执行
		public void onServiceDisconnected(ComponentName name) {
			servicedemo = null;
			System.out.println("onServiceDisconnected");
		}
	}
}


Service子类

package com.example.servicebinddemo;

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

public class ServiceDemo extends Service {
	//client service接口
	private final IBinder ibinder=new LocalBinder();
	public int i=1;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		System.out.println("creat service");
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("onbind");
		//返回的ibinder作为 activity 中
		//onServiceConnected(ComponentName name, IBinder service)参数,使用该参数获得service对象
		return ibinder;
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("onUnbind");
		return super.onUnbind(intent);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("onDestroy");
		super.onDestroy();
	}
	//
    public class LocalBinder extends Binder {
        ServiceDemo getService() {
        	//通过使用调用该方法获得service,((ServiceDemo.LocalBinder) service).getService();
            return ServiceDemo.this;
        }
    }
}


layout  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="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:onClick="startservice"
        android:text="Start" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="56dp"
        android:onClick="stopservice"
        android:text="Stop" />

</RelativeLayout>


manifest.xml    添加权限

<service android:name="ServiceDemo"></service>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值