Android学习之路--四大组件--Service

来源:互联网 发布:js实现word预览 编辑:程序博客网 时间:2024/06/04 18:31

四大组件

  • Activity
  • BroadCastReceiver
  • Service
  • ContentProvider

  • Service

            应用中,我们对其操作都是停留在视觉上的,就是用户只关注视觉上的东西,那都是在前台显示的。但是有些功能,我们不想在前台显示,在后台进行工作,而我想干点其他的事情,典型的例子就是听歌,我可以边听歌别玩游戏。这时候,这个听歌的过程就是在后台进行的了。

           还是按照之前的习惯,我们可以从它的生命周期开始在结合他的启动方式俩方面进行学习。

基本的程序是比不可少的。

public class TestService extends Service {    String tag = TestService.class.getSimpleName();    @Nullable    @Override    public IBinder onBind(Intent intent) {        Log.i(tag,"onBind");        return null;    }    @Override    public void onCreate() {        super.onCreate();        Log.i(tag,"oncreate");    }    @Override    public void onStart(Intent intent, int startId) {        super.onStart(intent, startId);        Log.i(tag,"onStart");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i(tag,"onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.i(tag,"onDestroy");    }}

然后在manifest配置我们的服务,然后我们开始启动。


  • 启动方式 startService方式启动,stopService停用
private void start() {    startService(new Intent(this, TestService.class));}

我们可以看到它的生命周期为:(第一次点击,即第一次启动)

.073 5356-5356/com.android.app I/TestService: oncreate.073 5356-5356/com.android.app I/TestService: onStartCommand.073 5356-5356/com.android.app I/TestService: onStart

启动之后我们执行上面的start方法:

.153 5356-5356/com.android.app I/TestService: onStartCommand.153 5356-5356/com.android.app I/TestService: onStart

我们看到在第一次启动之后,在启动它,就执行start的俩个方法,而在安卓6.0当中,onstart方法已经废弃了,不适合用了(但是生命周期还是要走的)。

然后我们停止服务:

private void stop() {    stopService(new Intent(this, TestService.class));}

那么它的生命周期是:

.655 5356-5356/com.android.app I/TestService: onDestroy

它直接就销毁了。


  • bindService启动,unbindService结束

在我们启动启动之前,在service类中加俩个方法

private  MyBind myBind = new MyBind(); @Nullable @Override public IBinder onBind(Intent intent) {     Log.i(tag,"onBind");     return myBind; }@Overridepublic boolean onUnbind(Intent intent) {    Log.i(tag,"onUnbind");    return super.onUnbind(intent);}class  MyBind extends Binder{    public MyBind(){    }}

Activity中加入如下方法

private ServiceConnection conn = new ServiceConnection() {    @Override    public void onServiceConnected(ComponentName name, IBinder service){     Log.i("info","onServiceConnected");   }    @Override    public void onServiceDisconnected(ComponentName name) {    }};private void start() {    Intent intent = new Intent(this,TestService.class);    intent.setAction("myService");    bindService(intent, conn,Context.BIND_AUTO_CREATE);}

manifest配置:

<service android:name=".TestService">    <intent-filter>        <action android:name="myService"></action>    </intent-filter></service>

这时,我们bindService启动。执行后,查看起生命周期:

.835 17056-17056/com.android.app I/TestService: oncreate.835 17056-17056/com.android.app I/TestService: onBind.835 17056-17056/com.android.app I/info: onServiceConnected

在完成后,我们在调用unbindService销毁:

unbindService(conn);

conn 就是我们上面创建的 ServiceConnect 创建的类。

它的生命周期为:

.918 2854-2854/com.android.app I/TestService: onUnbind.918 2854-2854/com.android.app I/TestService: onDestroy

这是它的销毁方式,bindserver之后,不用时我们记得要调用unBindService销毁,不然会报错,内存溢出。

这时,我们的service启动方式学习完毕。


0 0
原创粉丝点击