跟着第二行代码回顾Android-- service

我来谈谈我对服务的认识:就是可以把任务放到一个不需要界面的Activity来运行,又区别于Activity,所以就有了service。

比如下载和播放音乐都可以被放到服务里面。

启动服务有两种方式,一种是startService,一种是bindService。

1.启动和停止服务

Intent intent=new Intent(MainActivity.this,MyService.class);
        switch (view.getId()){
            case R.id.startService:
                startService(intent);//启动服务
                break;
            case R.id.stopService:
                stopService(intent);//停止服务
                break;
2.绑定和解绑服务

case R.id.bindService:
                bindService(intent,connection,BIND_AUTO_CREATE);
                break;
            case R.id.unbindService:
                unbindService(connection);
private MyService.DownloadBinder downloadBinder;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadBinder=(MyService.DownloadBinder)iBinder;
            downloadBinder.startDownload();
            downloadBinder.getProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d("csc","service unbind");
        }
    };
这里需要注意的是服务的onBind返回一个Binder对象,我们就可以在服务里面创建一个内部类,来继承Binder,然后在service中new一下,让这个类变成service的成员变量,这样就可以在服务被绑定到Activity的时候,调用Service的内部成员变量,从而来调用service里面的方法。

private DownloadBinder downloadBinder=new DownloadBinder();
    public MyService() {
    }
    class DownloadBinder extends Binder{
        public void startDownload(){
            Log.d("csc","startDownload");
        }
        public int getProgress(){
            Log.d("csc","getProgress");
            return 0;
        }
    }

注意一点绑定可以绑定多次,解绑只能解绑一次,再次解绑会崩溃。


服务的生命周期,startService的时候,先onCreate(),再onstart(),stopService的时候,onDestroy。

bindService的时候 onCreate(),再onBind,unBindService的时候,onDestroy();

两种方式可以混合,先startService,再BindService,接着UNBindService,最后stopService()

生命周期依次为 onCreate(),onStart(),接着onBind(),然后是Unbind(),最后是onDestroy()


提一点谷歌为我们提供了一个IntentService,来帮助我们处理耗时任务,把子线程创建和stopself()封装为了onHandleIntent方法。

为了方便查看任务下载进度,音乐播放情况,一般会做一个前台service。

在onCreate方法里面添加以下代码就可以了

 public void onCreate() {
        Log.d("csc","MyService onCreated");
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
        Notification notification=new NotificationCompat.Builder(this).setContentTitle("This is content title")
                .setContentText("This is content text").setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pi).build();
        startForeground(1,notification);
        super.onCreate();
    }






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值