Android service 服务简单创建与运用实例

定义类继承Service,然后在manifest.xml中注册

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

/**
 * 定义一个后台播放音乐的服务
 */
public class MusicService extends Service {

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("m_tag","MusicService == onBind");
        return new MyBinder();
    }

    class MyBinder extends Binder {
    //Activity绑定之后可以调用的方法
        public void play(){
            Log.e("m_tag","===play===");
        }
        public MusicService getService(){
            return MusicService.this;
        }

    }
    @Override
    public String toString() {
        return "MusicService ---- toString";
    }
    //第一次创建时触发
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("m_tag","MusicService == onCreate");
    }
    //每次startService都会触发
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //服务处理事情的业务
        //intent可以获取绑定传递过来的参数、序列化参数等……
        Log.e("m_tag","====onStartCommand===");
        return START_STICKY;
    }

    //销毁时触发
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("m_tag","====onDestroy===");
    }

    //接触绑定调用的方法
    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("m_tag","MusicService == onUnbind");
        return true;
    }
}

androidMainfest.xml文件中注册广播

     <service
            android:name=".MusicService"
            android:enabled="true"    //激活
            android:exported="true"> //公开
            <intent-filter
                <acion android:name="abc123"/>
                <data android:name="????"/>
            <intent-filter/>
        service>

然后我们就可以在Activity中使用或者进行绑定,调用里面相关的方法

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MusicService.MyBinder mBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
        findViewById(R.id.btn_bind).setOnClickListener(this);
        findViewById(R.id.btn_unbind).setOnClickListener(this);
    }
    //点击监听
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                //启动服务,第一次调用service的onCreate和 onStartCommand方法
                Intent it = new Intent(MainActivity.this,MusicService.class);
                startService(it);
                break;
            case R.id.btn_stop:
                //停止服务
                Intent it2 = new Intent(MainActivity.this,MusicService.class);
                stopService(it2);
                break;
            case R.id.btn_bind:
                //绑定服务
                Intent it3 = new Intent(MainActivity.this,MusicService.class);
                bindService(it3,conn, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btn_unbind:
                //接触绑定
                unbindService(conn);
                break;
        }
    }

    //服务连接器
    private ServiceConnection conn = new ServiceConnection() {
        //连接成功
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBinder = (MusicService.MyBinder) service;
            Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
            mBinder.play();
            mBinder.getService().toString();
        }
        //连接失败
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
}

两种用法

第一种是直接startService()的方法直接调用:
  //启动服务,第一次调用service的onCreate和 onStartCommand方法
  Intent it = new Intent(MainActivity.this,MusicService.class);
  startService(it);
第二种是绑定服务,进行调用里面的方法:
//需要在Service的onBind方法中返回一个IBinder对象
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("m_tag","MusicService == onBind");
        return new MyBinder();
    }

    class MyBinder extends Binder {
        public void play(){
            Log.e("m_tag","===play===");
        }

        public MusicService getService(){
            return MusicService.this;
        }
    }
      //绑定服务
      Intent it3 = new Intent(MainActivity.this,MusicService.class);
      bindService(it3,conn, Context.BIND_AUTO_CREATE);

      private ServiceConnection conn = new ServiceConnection() {……
      //接触绑定的方法
      unbindService(conn);

你可能感兴趣的:(server类别)