service 服务:
概念:
服务,作为Android四大组件之一,是一个没有用户界面的在后台运行执行耗时操作的应用组件。我们今天就来讲解一下有关服务的生命周期、两种开启方式以及相关用法。
分类
服务有两种开启方式,一种是正常开启, 一种是以绑定的方式开启,当然,这两种方式可以组成混合开启。
重点内容 3. 服务的正常启动
第一步:编写一个类,继承Service类;
package com.example.text;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService1 extends Service{
//重写IBinder,服务对象被绑定是回调
@Override
public IBinder onBind(Intent arg0) {
return null;
}
//重写onCreate,服务对象被创建时回调
@Override
public void onCreate() {
super.onCreate();
}
//重写onDestroy,服务对象被销毁时回调
@Override
public void onDestroy() {
super.onDestroy();
}
//重写onStartCommand,服务对象被启动时回调
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
//重写onUnbind,服务对象被解绑时回调
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
第二步:清单文件中注册服务
<service
android:name=".MyService1">//全路径
<intent-filter >
<action android:name="aa.bb" />
</intent-filter>
</service>
第三步:mainactivity里面开启服务和关闭服务
package com.example.text;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.button1){
Intent intent = new Intent("aa.bb");
this.startService(intent);
}else if(v.getId()==R.id.button2){
Intent intent = new Intent("aa.bb");
this.stopService(intent);
}
}
}
正常启动服务总结:
启动时:mainactivity里面开启服务,startService();
MyService里面:创建服务对象,启动服务对象,销毁服务对象;
结束时:mainactivity里面关闭服务,stopService();
重点内容4.服务绑定开启
第一步: 编写一个类,继承Service类;
服务绑定启动是我们想用到服务里面的一些数据,方法;我们通过什么方式传递呢?
这里就要涉及到 public IBinder onBind(Intent arg0)这个重写的方法了,这个回调返回值类型为IBinder,我们可以在mainactivity里面用 下面这个回调函数接收IBinder;
Myservicee:
public class MyBinder extends Binder {
public MyService getMyService() {
return MyService.this;
}
}
//当绑定服务时回调, 开子线程做费时操作
@Override
public IBinder onBind(Intent arg0) {
System.out.println("onBind 绑定服务");
new Thread(){
public void run(){
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress++;
if(progress>=100){
break;
}
}
}
}.start();
return new MyBinder();//返到值会被传递到ServiceConnection的方法public void onServiceConnected(ComponentName arg0, IBinder iBinder)中去
}
mainactivity:
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
//当绑定服务成功后被回调
@Override
public void onServiceConnected(ComponentName arg0, IBinder iBinder) {
}
};
第二步:清单文件中注册服务
<service android:name="xena.act.MyService">
<intent-filter >
<action android:name="farsight.xena.act.MyService"/>
</intent-filter>
</service>
第三步:mainactivity里面开启服务,绑定,解绑,关闭服务
public class MainActivity extends Activity {
private ProgressBar progressBar;
private MyService myService = null;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
//当绑定服务成功后被回调
@Override
public void onServiceConnected(ComponentName arg0, IBinder iBinder) {
Toast.makeText(MainActivity.this, "服务绑定成功", Toast.LENGTH_LONG).show();
MyBinder myBinder = (MyBinder) iBinder;
myService = myBinder.getMyService();
showPro();
}
};
private Handler handler = new Handler() {
public void handleMessage(Message msg) {//主线程
int pro = msg.arg1;//得到进度
progressBar.setProgress(pro);
}
};
public void showPro() {
new Thread(){
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = handler.obtainMessage(); //new Message();
msg.arg1 = myService.progress;//进度
handler.sendMessage(msg);//发送消息
if(myService.progress >= 100) {
break;
}
}
}
}.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
}
//绑定服务
public void bind(View v) {
Intent intent = new Intent();
intent.setAction("farsight.xena.act.MyService");
this.bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
//解绑服务
public void unBind(View v) {
this.unbindService(conn);
}
}