您的位置:首页 > 其它

绑定服务

2016-03-29 12:01 281 查看
1、绑定服务的作用:可以在其他地方调用服务里的方法

2、代码实现:

1)视图

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:onClick="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启服务" />

<Button
android:onClick="stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务" />

<Button
android:onClick="change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变歌曲" />

<Button
android:onClick="bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务" />

<Button
android:onClick="unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="断开绑定" />

</LinearLayout>


2)清单文件

<service android:name=".SongService"></service>


3)activity代码,主要用来绑定和解除绑定的

package com.example.songservice;

import com.example.songservice.SongService.MyBinder;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

private SongService.MyBinder mybinder;
private Myconn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void start(View view){
Intent intent = new Intent(this, SongService.class);
startService(intent);
}

public void stop(View view){
Intent intent = new Intent(this, SongService.class);
stopService(intent);
}

public void change(View view){
//关于直接new对象不行的原因?因为在开启服务时会创建与之对应的上下文;而如果采用new对象的方式,是没有上下文的
mybinder.changeSong("最炫民族风");
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub

try {
unbindService(conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

super.onDestroy();
}

public void bind(View view){
Intent intent = new Intent(this, SongService.class);
conn = new Myconn();
//第一个参数,说明要开启的服务
//第二个参数,中间人
//第三个参数,绑定服务时,如果服务不存在,则自动创建
bindService(intent, conn, BIND_AUTO_CREATE);
}

public void unbind(View view){
unbindService(conn);
}

//与服务联系的回调对象
private class Myconn implements ServiceConnection{
//在服务被绑定成功时调用的方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("代理人被返回回来");
mybinder = (MyBinder)service;
}

//在服务失去绑定时调用的方法,只有在程序异常终止才调用
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

}

}


4)服务类的代码

package com.example.songservice;

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

public class SongService extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("春哥的服务被绑定了");
return new MyBinder();
}

//代理人;如果把public改为private,则可以使用接口进行代替
public class MyBinder extends Binder{
public void changeSong(String name){
change(name);
}
}

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

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("开始唱歌");
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("停止唱歌");
}

public void change(String songName){
Toast.makeText(getApplicationContext(), "改唱歌曲为" + songName, 0).show();
}

}


注意事项:

1、如果已经绑定了服务,并且不解除绑定,直接退出activity,就会报错;除非先解除绑定,然后再退出
2、如果以绑定服务的方式开启服务,则解除绑定时服务会停止
3、如果以绑定服务的方式开启服务,直接退出,则会报错;并且服务会被销毁
4、正常的方法是:开启服务->绑定服务->解除绑定
5、多次解除绑定程序会报错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: