android服务之启动方式

服务有两种启动方式

  • 通过startService方法来启动
  • 通过bindService来开启服务

布局文件


在布局文件中我们定义了四个按键来测试这两种方式来开启服务的不同

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="开始服务"
        android:onClick="start"/>
    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="停止服务"
        android:onClick="stop"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="绑定服务"
        android:onClick="bind"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="解绑"
        android:onClick="unbind"/>
</LinearLayout>

Activity


在Activity中我们使用了这两种方式来开启服务

package xidian.dy.com.chujia;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Intent service;
    MyConnection mc;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        service = new Intent(this, MyService.class);
        mc = new MyConnection();
    }

    public void start(View v){
        Toast.makeText(this,"开启服务",Toast.LENGTH_SHORT).show();
        startService(service);
    }

    public void stop(View v){
        Toast.makeText(this,"关闭服务",Toast.LENGTH_SHORT).show();
        stopService(service);
    }

    public void bind(View v){
        bindService(service,mc,BIND_AUTO_CREATE);
    }

    public void unbind(View v){
        unbindService(mc);
    }

    class MyConnection implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}

注意在绑定服务的时候需要传入回调函数(第二个参数),当绑定成功会调用onServiceConnected方法,当解除绑定会调用onServiceDisconnected方法。

Service


在服务代码中我们将各个方法复写了一边,其实代码中各个方法也表示了服务的生命周期。

package xidian.dy.com.chujia;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by dy on 2016/7/12.
 */
public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(this.getClass().getName(), "****start方法****");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(this.getClass().getName(), "*****startCommand方法*****");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.i(this.getClass().getName(), "***Destroy方法***");
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(this.getClass().getName(), "****Bind方法被调用****");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(this.getClass().getName(),"****unBind方法被调用****");
        return super.onUnbind(intent);
    }
}

两者区别


启动服务

  • 点击开始服务

  • 点击返回按键销毁Activity,我们发现服务仍在运行

  • 点击停止服务

 绑定服务

  • 点击绑定服务

  • 点击返回按键销毁Activity,则会抛出异常并且有两方法被调用

 通过对比我们发现startService开启的服务是独立运行的,而通过bindService开启的服务则依赖于Activity,但Activity被销毁时绑定的服务也被销毁了

转载于:https://www.cnblogs.com/xidongyu/p/5669567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值