业务

Service

后台执行耗时操作,例如音乐。如果使用Activity来播放音乐,可能会在程序切换或者页面消失时停止,但是使用Service则不会。

与thread的区别

service:长时间执行,不与用户发生交互
thread: 需要进行交互。如某个Acitvity的背景音乐。

使用

startService() 调用者与被调用者没有联系,就算调用者消失,service也会继续。
bindService() 调用者与被调用绑定关系,调用者消失service就会停止。

startService
public class ExampleService extends Service {

    private static final String TAG = "ExampleService";

    //    @androidx.annotation.Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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


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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"start");
        return super.onStartCommand(intent, flags, startId);
    }
}
private View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startButton:
                Intent startIntent = new Intent(MainActivity.this, ExampleService.class);
                startService(startIntent);
                break;
            case R.id.stopButton:
                Intent stopIntent = new Intent(MainActivity.this, ExampleService.class);
                stopService(stopIntent);
                break;
            default:
                break;
        }
    }
}
bindService
public class BinderService extends Service {

    private static final String TAG = "BinderService";
    private Mybinder binder = new Mybinder();

    public void myMethod() {
        Log.i(TAG, "my method");
    }

    public class Mybinder extends Binder {
        public BinderService getService(){
            return BinderService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

}
private View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startBinderService:
                bindMyService();
                break;
            case R.id.stopBinderService:
                unbindMyService();
                break;
            default:
                break;
        }
    }

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            BinderService.Mybinder binder = (BinderService.Mybinder)service;
            BinderService myService = binder.getService();
            myService.myMethod();
            isConnected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isConnected = false;
        }
    };

    private void bindMyService() {
        Intent intent = new Intent(BinderActivity.this, BinderService.class);
        bindService(intent, conn , Context.BIND_AUTO_CREATE);

    }

    private void unbindMyService() {

        if (isConnected) {
            Log.i(TAG, "unbind method");
            unbindService(conn);
        }
    }
};
线程问题

service会占用主线程,如果进行耗时操作会卡线程,所以要使用多线程。多线程的使用方法如下。

private class MyThread extends Thread {
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
//调用
new MyThread().start();

IntentService

区别于serviceIntentService是异步,不需要自己创建线程。

public class ExampleIntentService extends IntentService {

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public ExampleIntentService() {
        super("ExampleIntentService");
    }

    @Override

    protected void onHandleIntent(Intent intent) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(业务)