StartService和BindService

StartService启动方式
Intent intent1=new Intent(this, MyIntentService.class);
Bundle bundle = new Bundle();
bundle.putString("intentparam", "oper1");
intent1.putExtras(bundle);startService(intent1);
BindService启动方式
Intent intent1=new Intent(this, MyIntentService.class);
Bundle bundle = new Bundle();
bundle.putString("intentparam", "oper2");
intent1.putExtras(bundle);
bindService(intent1,conn,BIND_AUTO_CREATE);
    //bindService第二个参数  ,用于和Service进行通信

MyIntentService services;//从ServiceConnection 中获取的service实例,调用service对外暴露的方法

  private ServiceConnection conn=new ServiceConnection() {    
      @Override    
      public void onServiceConnected(ComponentName name, IBinder service) {        
          MyIntentService.MyBinder binder= (MyIntentService.MyBinder) service;         
           services=binder.getService();       
           int randomNumber = services.getRandomNumber();        
           Log.v(TAG,"数据:"+randomNumber);   
     }    
      @Override    
    public void onServiceDisconnected(ComponentName name) {    }};
Service类
public class MyIntentService extends IntentService {
    private static final String TAG = "MyIntentService";
    private final Random generator = new Random();
    private MyBinder binder = new MyBinder();

    public MyIntentService() {
        super("MyIntentService");
    }

    public MyIntentService(String name) {
        super(name);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(TAG, "onBind");

        return binder;
    }

    @Override
    public void onCreate() {
        Log.v(TAG, "onCreate");
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.v(TAG, "onStart");
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "onStartCommand");

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean bindService(Intent service, ServiceConnection conn, int flags) {
        Log.v(TAG, "bindService");

        return super.bindService(service, conn, flags);
    }

    @Override
    public void setIntentRedelivery(boolean enabled) {
        Log.v(TAG, "setIntentRedelivery");
        super.setIntentRedelivery(enabled);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String intentparam = intent.getExtras().getString("intentparam");

        if (intentparam.equals("oper1")) {
            Log.v(TAG, "Operation1");
        } else if (intentparam.equals("oper2")) {
            Log.v(TAG, "Operation2");
        }

        try {
            Thread.sleep(2000);
            Log.v(TAG, "线程名字:" + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        Log.v(TAG, "onDestroy");
        super.onDestroy();
    }

    //getRandomNumber是Service暴露出去供client调用的公共方法
    public int getRandomNumber() {
        return generator.nextInt();
    }

    public class MyBinder extends Binder {
        public MyIntentService getService() {
            return MyIntentService.this;
        }
    }
}

onHandleIntent是中维护和一个handleThread,通过参数判断不同的任务,service可以多次调用,handlethread会一个一个执行

你可能感兴趣的:(StartService和BindService)