Service学习笔记

1 定义服务
public class MyService extends Service {
    public MyService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
2 启动和停止服务
@Override
    public void onCreate() {
        Log.d(TAG,"onCreate executed");
        super.onCreate();
    }

    @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() {
        Log.d(TAG,"onDestroy executed");
        super.onDestroy();
    }
case R.id.start_service:
                Intent startMySevice = new Intent(this, MyService.class);
                startService(startMySevice);
                break;
            case R.id.stop_service:
                Intent stopMyService = new Intent(this, MyService.class);
                stopService(stopMyService);
                break;
3 活动和服务进行通信
private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
case R.id.bind_service:
                Intent bindMySevice = new Intent(this, MyService.class);
                bindService(bindMySevice, connection, BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(connection);
4 服务的生命周期

1)一旦在项目的任何位置调用了Context的startService() 方法,相应的服务就会启动起来并回调 onStartCommand() 方法。如果这个服务之前还没有创建过,onCreate() 方法会先于 onStartCommand() 方法执行。复制启动了就会一直保持运行状态,直到 stopService() 或 stopSelf() 方法被调用。注意,素日每调用一次startService方法,onStartCommand() 就会执行一次,但实际上每一个服务只会存在一个实例,所以不管你调用了所少次 startService() 方法,只需要调用一次 stopService() 或 stopSelf() 方法,服务就会停止下来了。

2)调用Context的 bindService() 来获取一个服务的持久连接,这时就会回调服务中的 onBind() 方法。类似的,如果这个服务之前还没有创建过,onCreate() 方法就会先于onBInd() 方法执行。之后,调用方可获取到onBInd() 方法里返回的IBinder对象的实例,这样就可以通信了。只要调用方和服务之间的连接没有断开,服务就会一直保持运行状态。

3)当调用了 startService() 方法后,又去调用了 stopService() 方法,这时服务中的 onDestroy() 方法就会执行,表示服务已经销毁了。类似的,当调用了 bindService() 后又去调用unbindService(),onDestroy() 方法也会执行。

4)如果我们对一个服务既调用了 startService() 又调用了 bindService() 方法后,根据Android 系统的机制,一个服务只要启动或被绑定了之后,就会一直处于运行状态,必须要让以上两种条件同事不满足,服务才会被销毁。所以,要同时调用 stopService() 和 unbindService() 方法,onDestroy() 才会执行。

5 前台服务
@Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG,"onCreate executed");
        Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this,0, intent,0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("This is title.")
                .setContentText("This is text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentIntent(pi)
                .build();
        startForeground(1, notification);
    }

startService() 或 bindService() ,系统通知栏都会显示一个通知图标,下拉状态栏可以看到该通知内容。

6 IntentService

为了简单的创建一个异步的、会自动停止的服务,Android提供了IntentService类。

public class MyIntentService extends IntentService {

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

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("MyIntentService","Thread id is: " + Thread.currentThread().getId());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyIntentService","onDestroy executed.");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值