经过进一步学习发现昨天的程序(下面的程序)存在问题
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>