zoukankan      html  css  js  c++  java
  • android87 Service

    ----------------------------------------------------------------------------
    #Service(服务和Activity是运行在进程中的,一个进程可以有多个服务,)
    * 就是默默运行在后台的组件,可以理解为是没有前台的activity,适合用来运行不需要前台界面的代码(例如下载就是在后台进行的,activity里面也可以开启一个线程来下载,即使返回到主页面线程也会继续下载,但是Activity到后台后就是一个后台进程很容器被杀掉,Activity被杀掉了那么这个子线程也就杀掉了,所以不能在Activity里面做下载而是用Service做下载,服务进程很难被杀死,即使杀死了也会重启。)
    * 服务可以被手动关闭,不会重启,但是如果被自动关闭,内存充足就会重启
    * startService启动服务的生命周期
        * onCreate-onStartCommand-onDestroy只有这3个,
    * 重复的调用startService会导致onStartCommand被重复调用
    
    ----------------------------------------------------------------------------
    # 进程优先级
    1. 前台进程:拥有前台的正在与用户交互的Activity(onResume方法被调用)
    2. 可见进程:拥有可见但是没有焦点的Activity(onPause方法被调用,被遮挡或者部分遮挡)
    3. 服务进程:Service,通过startService方法启动的服务,不到万不得已不会被回收,而且即便被回收,内存充足时也会被重启,如果是手动杀死或者代码杀死就不会重启。
    4. 后台进程:不可见的后台的activity(activity的onStop方法被调用了),很容易被回收
    5. 空进程:没有拥有任何活动的应用组件Activity的进程,没有运行任何activity,很容易被回收
    package com.itheima.startservice;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        private Intent intent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            intent = new Intent(this, MyService.class);
        }
    
    
        public void click(View v){
            //显式启动服务
            startService(intent);
        }
        public void click2(View v){
            //关闭服务
    //        Intent intent = new Intent(this, MyService.class);
            stopService(intent);
        }
        
    }
    
    //清单文件:显示启动服务
    //<service android:name="com.itheima.startservice.MyService"></service>
    package com.itheima.startservice;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    
    public class MyService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            System.out.println("create方法");
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            System.out.println("startCommand方法");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            System.out.println("destroy方法");
        }
    
        
    }
  • 相关阅读:
    问题记录之spring-mvc.xml配置文件报错
    问题记录之用poi生成图片并插入到word时,图片中的中文显示不出来
    问题记录之每次打开vmware的时候都会跳出xftp的安装程序问题解决
    DataTable和List之间互转
    .NET[C#]中实现实体对象深拷贝(克隆/复制)的几种方法
    SQL Server表分区详解(转)
    vs2017使用GitHub插件发布项目到github
    jquery autocomplete 在IE11中出现打开网页输入框有默认值时下拉列表是已经打开的状态解决
    MVC DropDownLis 二级联动实现
    sql 查询某个表在哪些存储过程(SP)中使用到
  • 原文地址:https://www.cnblogs.com/yaowen/p/4948775.html
Copyright © 2011-2022 走看看