以简单的播放背景音乐服务为例:
public class Music_Service extends Service {
MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
//这个方法暂时还没有搞懂如何去用,就这个例子来说不起任何作用
@Override
public void onCreate() {
// Service主代码
super.onCreate();
player = MediaPlayer.create(this, R.raw.music);
player.start();
}
@Override
public void onDestroy() {
// Service销毁时执行的代码
player.stop();
super.onDestroy();
}
}
启动服务:
Intent i = new Intent(MainActivity.this, Music_Service.class);
//基于Intent启动服务,声明Intent对象并设置服务的位置
startService(i);
//启动服务
关闭服务:
stopService(i);
//停止服务,注意i与上面启动服务时的i对应
这种简单的Service貌似不能传递参数,实现直接从Activity到Service传递数据需要使用IntentService
一个简单的IntentService:
public class Music_IntentService extends IntentService {
public Music_IntentService() {
super("");
}
//必须要重写无参数的构造方法
protected void onHandleIntent(Intent i) {
// IntentService主代码
// i即是从其他Activity传来的
Log.e("msg",i.getStringExtra("msg"));
}
}
启动服务:
Intent i = new Intent(MainActivity.this, Music_IntentService.class);
i.putExtra("msg", msg);
//给Intent绑定数据
startService(i);
关闭服务 :与上述关闭服务相同
另外还想研究的是从Service传递参数给Activity,有个思路是通过ActivityResult来实现,有待进一步研究
当我们用手机下载东西的时候,需要用到后台服务,如果我们想查找当前的任务下载进度的话,就需要我们从后台服务中获取数据,那么我们如何更快捷的去获取服务的数据呢?
答案:绑定服务
①主页面(几个简单的按钮):activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bind" />
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unbind" />
<Button
android:id="@+id/getServiceStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/getServiceStatus" />
</LinearLayout>
②MainActivity:
public class MainActivity extends Activity {
Button bind, unbind, getServiceStatus;
BindService.MyBinder binder;
//创建服务绑定对象
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (MyBinder) service;
}
};
//创建连接服务的对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind = (Button) findViewById(R.id.bind);
unbind = (Button) findViewById(R.id.unbind);
getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
final Intent intent = new Intent();
intent.setAction("SICT.SERVICE.BIND_SERVICE");
//给Intent设置动作
bind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
bindService(intent, conn, Service.BIND_AUTO_CREATE);
//绑定服务
}
});
unbind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
boolean bl = binder.getQuit();
if (!bl) {
unbindService(conn);
//解除绑定
}
}
});
getServiceStatus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this,
"当前下载进度" + binder.getData() + "%", Toast.LENGTH_LONG).show();
}
});
}
}
③BindService.java
public class BindService extends Service {
int num = 0;
public MyBinder binder = new MyBinder();
//创建服务绑定对象
boolean quit;
@Override
public IBinder onBind(Intent arg0) {
System.out.println("Service is Binded");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
this.quit = true;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Toast.makeText(BindService.this, "Service is ReBinded",
Toast.LENGTH_LONG).show();
}
@Override
public void onCreate() {
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
num++;
}
}
}).start();
}
public class MyBinder extends Binder {
public String getData() {
return num + "";
}
public boolean getQuit() {
return quit;
}
}
}