当前位置: 首页 > news >正文

android89 服务service

 
  

#服务

服务不能new,new出来的只是一个普通java对象不是服务,只能够通过Intent和startService(intent)创建服务。 ###开启方式 * startService,onCreate-onStartCommand- onDestroy没有与绑定相关的方法。 * 该方法启动的服务所在的进程属于服务进程, * Activity一旦启动服务,服务就跟Activity一毛钱关系也没有了,只不过运行在同一个进程而已。 * bindService,onCreate-onBind-onUnbind- onDestroy。 * 绑定后再绑定不会执行任何操作,解绑后再解绑会出异常。 * 该方法启动的服务所在进程不属于服务进程 * Activity与服务建立连接,Activity一旦死亡,服务也会死亡
复制代码
复制代码
package com.itheima.runservice;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    private Intent intent;
    private MyServiceConn conn;

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(this, MyService.class);
        conn = new MyServiceConn();
    }


    public void start(View v){
        startService(intent);
    }
    
    public void stop(View v){
        stopService(intent);
    }
    
    public void bind(View v){
        //绑定服务
        bindService(intent, conn, BIND_AUTO_CREATE);
    }
    
    public void unbind(View v){
        //解绑服务
        unbindService(conn);
    }
    
    class MyServiceConn implements ServiceConnection{
        //服务连接成功时,此方法调用
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        }

        //服务失去连接时,此方法调用
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }
}
//清单文件<service android:name="com.itheima.runservice.MyService"></service>
复制代码
复制代码
package com.itheima.runservice;

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
        System.out.println("bind方法");
        return null;//返回Activity和MyService的中间人对象
    }

    //解绑时调用
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("unbind方法");
        return super.onUnbind(intent);
    }
    
    @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("start方法");
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        System.out.println("destroy方法");
        super.onDestroy();
    }
    
}
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4951686.html,如需转载请自行联系原作者


相关文章:

  • 阿里云服务器使用之一:搭建jsp服务器
  • 安装 virtualenv
  • 实例详解ISA防火墙策略元素:ISA2006系列之五
  • 调查称没互联网35.8%的人将手足无措,您呢?
  • 实现.NET Core配置Provider之EF
  • hbase shell 启动报错
  • Ubuntu 16下安装zabbix agent
  • python2中MySQLdb加入超时及其重试功能
  • Redis实战(1)入门和适用场合
  • dell 1088 安装 windows xp 蓝屏的解决方法
  • Apache服务器之------https功能
  • mongodb3.4.4安装副本集,wt引擎配置优化(二)
  • PLSql使用
  • Tomcat
  • 44、BGP配置实验之Weight选路
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • Asm.js的简单介绍
  • Go 语言编译器的 //go: 详解
  • linux学习笔记
  • Linux中的硬链接与软链接
  • oldjun 检测网站的经验
  • SQLServer之创建显式事务
  • vue自定义指令实现v-tap插件
  • WebSocket使用
  • 分类模型——Logistics Regression
  • 官方解决所有 npm 全局安装权限问题
  • 每天10道Java面试题,跟我走,offer有!
  • 前言-如何学习区块链
  • 算法-图和图算法
  • 写给高年级小学生看的《Bash 指南》
  • Mac 上flink的安装与启动
  • Nginx实现动静分离
  • NLPIR智能语义技术让大数据挖掘更简单
  • ​第20课 在Android Native开发中加入新的C++类
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #Z0458. 树的中心2
  • $.ajax,axios,fetch三种ajax请求的区别
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • (转)socket Aio demo
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .Net 垃圾回收机制原理(二)
  • .NET精简框架的“无法找到资源程序集”异常释疑
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • .NET与 java通用的3DES加密解密方法
  • .net中生成excel后调整宽度
  • .py文件应该怎样打开?
  • .sys文件乱码_python vscode输出乱码
  • [20150707]外部表与rowid.txt
  • [20171101]rman to destination.txt
  • [AI]ChatGPT4 与 ChatGPT3.5 区别有多大
  • [Android] Implementation vs API dependency