文章标题 android服务Service 的一些基本简介和用法

来源:互联网 发布:淘宝蓝海龙腾黑不黑 编辑:程序博客网 时间:2024/05/16 17:48

1、服务简介
执行耗时操作(后台),耗时操作不能写到了主线程, 需要一个子线程进行耗时操作

服务和Thread的区别?1.服务不是单一的进程。服务没有自己的进程,应用程序可以不同,服务运行在相同的进程中。2.服务不是线程。可以在线程中工作。
一、服务的生命周期总共有4个,    onCreate  onStartCommand  onDestroy onBind二、本地服务    onStartCommand返回值3三、服务的停止    stopService    stopSelf        stopSelf()主要是当一个进程停止后,后面的进程也停止;        stopSelf(id)主要是所有进程都停止

在MainActivity中的一些用法,主要有onCreate,start,stop

 protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        intent = new Intent(this,MyServices2.class);    }    public void start(View view){        intent.putExtra("data","下载的路径");        startService(intent);    }    public void stop(View view){        stopService(intent);    }
0 0
原创粉丝点击