Android Service详解(二)第一个Service

    Service中有四个重要函数:  

    public IBinder onBind(Intent arg0);    //必须实现,返回接口给Service
    public void onCreate();                //Service创建时调用
    public void onStart(Intent intent,int startId);//通过startService()会调用
    public void onDestroy();                //销毁时StopService()调用

 

通过StartActivity()函数启动Service,当第一次调用时会分别调用onCreate()和onStart在();

之后只会调用onStart();

通过函数StopService()结束Service,会调用onDestroy();

调用BindService():当Service未创建时调用onCreate()和onBind();当创建了只调用onBind();

使用函数bindService()和函数unbindService()可以绑定和解除绑定

对已经绑定的Service调用bindService()无效,即多次调用bindService()和调用一次bindService()一样。 unbindService()只能使用一次,即对于一个绑定的Service,只能调用一次unbindService(),多次调用会产生错误


该函数原型为:

bindService(Intent,ServiceConnection,BIND_AUTO_CREATE);

ServiceConnection是一个服务连接类,必须实现以下两个函数:

public void onServiceConnected(ComponentName arg0, IBinder arg1)//连接成功时调用
public void onServiceDisconnected(ComponentName arg0)        //连接失败时调用

    示例如下:

private ServiceConnection conn=new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName arg0, IBinder arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "success", Toast.LENGTH_LONG).show();
        Log.i("SERVICE","success");
    }
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "errer", Toast.LENGTH_LONG);
        Log.i("SERVICE","errer");
    }

    

    

Service实例:

    MainActivity.java:

private ServiceConnection conn=new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName arg0, IBinder arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "success", Toast.LENGTH_LONG).show();
        Log.i("SERVICE","success");
    }
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "errer", Toast.LENGTH_LONG);
        Log.i("SERVICE","errer");
    }
};
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1=(Button)this.findViewById(R.id.btn1);
    Button button2=(Button)this.findViewById(R.id.btn3);
    Button button3=(Button)this.findViewById(R.id.btn4);
    Button button4=(Button)this.findViewById(R.id.btn5);
    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startService(new Intent(MainActivity.this,NewService.class));
        }
    });
    button2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            stopService(new Intent(MainActivity.this,NewService.class));
        }
    });
    button3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            bindService(new Intent(MainActivity.this,NewService.class),conn,BIND_AUTO_CREATE);
        }
    });
    button4.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            unbindService(conn);
        }
    });
}

NewService.java:

public class NewService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(NewService.this, "onBind", Toast.LENGTH_LONG).show();
        Log.i("SERVICE","onbind");
        return null;
    }
    public void onCreate() {
        super.onCreate();
        Log.i("SERVICE","oncreat");
        Toast.makeText(NewService.this, "onCreat", Toast.LENGTH_LONG).show();
    }
    public void onStart(Intent intent,int startId) {
        Log.i("SERVICE","onstart");
        Toast.makeText(NewService.this, "onStart", Toast.LENGTH_LONG).show();
    }
    public void onDestroy() {
        Log.i("SERVICE","ondestory");
        Toast.makeText(NewService.this, "onDestory", Toast.LENGTH_LONG).show();
    }
}

Activity.xml

<?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:orientation="vertical" >
    <Button android:id="@+id/btn1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start"/>
    <Button android:id="@+id/btn3" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="stop"/>
    <Button android:id="@+id/btn4" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bind"/>
    <Button android:id="@+id/btn5" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unbind"/>    
</LinearLayout>

AndroidManifest.xml增加:

 <service android:name="com.example.new1.NewService"/>




你可能感兴趣的:(android,service)