Service作为内部类使用方法

最近公司有需求 service要以内部类方式使用特此记录

代码:

public class OutService extends Service {
    public OutService() {
        Log.d("OutService","OutService()");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.d("OutService","onBind(Intent intent)");
        throw new UnsupportedOperationException("Not yet implemented");
    }

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

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

        startService(new Intent(this,InnerService.class));

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

    /**
     * 内部 Service
     */
    public static class InnerService extends Service {
        public InnerService() {
            Log.d("InnerService","InnerService()");
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            Log.d("InnerService","onBind(Intent intent)");
            throw new UnsupportedOperationException("Not yet implemented");
        }

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

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

清单文件:

<service

    android:name=".service.OutService"

    android:enabled="true"

    android:exported="true"/>

<service android:name=".service.OutService$InnerService" />



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值