Service记住它

2018-07-24  本文已影响17人  Small_Cake
一,首先,就是启动一个Service了

1.1创建一个service类

public class SmallDownloadService extends Service {

    public static final String TAG = "SmallDownloadService";

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

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

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

}

1.2 在AndroidManifest.xml中声明service

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/cake"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/cake"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <service android:name="com.smallcake.okhttp.SmallDownloadService"/>

    </application>

1.3 启动这个service

Intent startIntent = new Intent(this, SmallDownloadService.class);
                startService(startIntent);
start.png start2.png runing.png

1.4 停止service

Intent stopIntent  = new Intent(this, SmallDownloadService.class);
                stopService(stopIntent );
stop.png
二,与Activity建立关联,在服务里面做点事情,绑定服务

2.1 让Activity与Service建立关联onBind()

public class SmallDownloadService extends Service {

    public static final String TAG = "SmallDownloadService";

    private MyBinder mBinder = new MyBinder();

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

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

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

    class MyBinder extends Binder {

        public void startDownload() {
            Log.d(TAG, "startDownload() executed");

        }

    }

}

2.2 绑定服务

Intent bindIntent = new Intent(this, MyService.class);
            bindService(bindIntent, connection, BIND_AUTO_CREATE);
 SmallDownloadService.MyBinder mBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {}
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBinder = (SmallDownloadService.MyBinder) service;
            mBinder.startDownload();
        }
    };
image.png bind and do something.png

2.3 取消绑定

 unbindService(connection);
unbind.png image.png image.png
 if(isServiceRunning(this,"com.smallcake.okhttp.SmallDownloadService"))unbindService(connection);
上一篇 下一篇

猜你喜欢

热点阅读