pasting

1    进程的概念

    [1]Android下四大组件都运行在主线程下
    [2]服务是在后台下运行    没有界面的activity   
           service 是 activity的大爷
进程的优先级
    [1]Foreground process    前台进程    优先级最高    相当于activity执行了onResume方法    用户正在交互
    [2]Visible    process    可视进程    一直影响用户看得见相当于activity执行了onPause方法
    [3]service    process    服务进程    通过startService方法开启一个服务
    [4]Background    process        后台进程    相当于activity执行了onStop方法    界面不可见    但是activity并没有销毁
    [5]Empty    process    空进程    不会维持任何组件运行
2    start方式开启服务的特点
    [1]定义四大组件的方式是一样的
    [2]定义一个类继承Service
    [3]第一次点击按钮开启服务    服务执行onCreate方法和onStartCommand方法
    [4]第二次点击按钮    再次开启服务    服务执行onStartCommand方法
    [5]服务一旦被开启    服务就会后台长期运行    直到用户手工停止
补充:股票应用
3    电话qie听器案例
    TelephoneManager
实现步骤
    [1]定义一个服务    开启服务    记得在清单文件配置服务
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAACZCAIAAAANXipBAAAXXklEQVR4nO3dTY/cxp3Hcb4YH/0K+urXMO/A6FcgBH2bOewl2AChFtBedPDCxnobELJCAqnjbOAOpFXWysherey2lLghKZI9kd1jSbESj0ayA+6Bze5i1b8e2Q9kz/eDAWw1i8Uihw+/LhY5WRFiMhws5GNt4s/f/eC/Jl/d+ub0k29O3njzrVvfnNz65uQ3k6Ofv/vB6emptc7R+f1sbz/buzQSJx9eyvbKAhfyo6Io7vbn/9zvXT5ezn7u2lSYZT/b2+8fLiYc5+eqz8/fVZZh+VxftG56+YK6oGpZVW1qk2rl5drWYdTPsl4+FabkvSzr9Xq9XPi8r/8mpnkvM0pWE7LMtozFjOV/prXPawsxCgRNqoz6QptX0v6mLNu/WqzcanOFY8unmC8jqKapuJcURVGusf2XEbo/OFZqFftDYdnPl5MyY/dMWt/59Dmh2QcHB+52NiwPYFWyqNKzcT4YDAbDifrhT372/nDy9I0331J/hpOnP/nZ+ysISVXWqUJSlWaE2ZWQpCSk0jIPLSYtsk6Zum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Owlzn0tWNrOkxvm9F4dZlQ5Nqan9lZ7iGpg54+7c8LcoQ70WJ/y6y3xMMqqzUk8KXfKNTh9G3QgFnpCBlmv/skVMW0pMMaMjxUrzNk2CHU7L0xJGvMqGctiHgO2rzVB/9KKbVAhEJZp+gzdlfASh6Nq2fM7qFoXdxd4p9I8RNoM7Q2ozisMltkRvWaIbFmuMzb2SwnxQvHQ5GqSqPU6BC3VXNb5IwTkrYxshTON+dZY9RsoSqUj7T4nlm9UY/L2b5CZI8Xdvz2Pb91fIW3i89KCrsm5XDV7HIO9eyrpVj4uTW/ZaYtsOn9q63DEcFIN4/Z91rXm9B0bajsLuPX9vrda0zJdquGGp8rcSRNjEi7zYfuvibhI51SIWmajdZdQIkepGLueaZVq2G8yk5pLk7FUa9aOLfB29EiS2+UasbavOz8oN0l1QIn7/G2EdO1c+6G1YxR6N48ntsdpGEfpdEl697Rr4aF/IjU/cORnqWbCWhdbqtoQEC/T8SZlwkoeSub2T3YXfnsg+kUH4P6kS0ilTHbYdUKLRoxRraomu3GqFYPsaM5M3ujza71xsDom/U6f/tsvRwiGrmSojy3xumq2d0cOrqv5oOnDAOwCms8DHdt3tFmAN6L6nZ5mt/ibwJZA8AqvONj49/RZgAAoEDWALA0xQS39ta8o80AAGAAWQMAAACAnQBZAwAAAICdAFkDAAAAgJ0AWQMAAACAnQBZAwAAAICdsI2sYc9D33G9AAAAAHgCi8ua/kGEDgEBWQMAAACAxVnLW2M5V2JN5p3FDQAAAIB35z+YOrK8GUBhFwAAAABJRU5ErkJggg==" alt="" />
    [2]在服务的onCreate方法里面    获取TelephoneManager
  1. // [1]获取teltphonemanager的实例
  2. TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    [3]注册电话的监听
  1. / [2]注册电话的监听
  2. tm.listen(new MyPhoneStateListener(),
  3. PhoneStateListener.LISTEN_CALL_STATE);
    [4]定义一个类用来监听电话的状态
  1. // 定义一个类用来监听电话的状态
  2. private class MyPhoneStateListener extends PhoneStateListener {
  3. // 当电话设置状态发生改变的时候调用
  4. @Override
  5. public void onCallStateChanged(int state, String incomingNumber) {
  6. // [3]具体判断一下电话的状态
  7. switch (state) {
  8. case TelephonyManager.CALL_STATE_IDLE:// 空闲状态
  9. if(recorder!=null){
  10. recorder.stop();
  11. recorder.reset(); // You can reuse the object by going back to
  12. // setAudioSource() step
  13. recorder.release(); // Now the object cannot be reused
  14. }
  15. break;
  16. case TelephonyManager.CALL_STATE_OFFHOOK:// 接听状态
  17. System.out.println("开始录音");
  18. recorder.start(); // Recording is now started
  19. break;
  20. case TelephonyManager.CALL_STATE_RINGING:// 响铃状态
  21. System.out.println("准备录音机");
  22. try {
  23. //[1]创建MediaRecorder实例
  24. recorder = new MediaRecorder();
  25. //[2]设置音频的来源
  26. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  27. //[3]设置输出的格式
  28. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  29. //[4]设置音频的编码方式
  30. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  31. //[5]设置存放的路径
  32. recorder.setOutputFile("/mnt/sdcard/luyin.3gp");
  33. //[6]准备录
  34. recorder.prepare();
  35. } catch (IllegalStateException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. } catch (IOException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. break;
  43. }
  44. super.onCallStateChanged(state, incomingNumber);
  45. }
  46. }
   [5]加权限
  1. <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
   [6]把开启服务的逻辑放到手机重启的广播中
  1. package com.phone.phonelistener;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. public class BootReceiver extends BroadcastReceiver {
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. //服务开启
  9. Intent intent1 = new Intent(context,PhoneService.class);
  10. context.startService(intent1);
  11. }
  12. }
  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  1. <application
  2. android:allowBackup="true"
  3. android:icon="@drawable/ic_launcher"
  4. android:label="@string/app_name"
  5. android:theme="@style/AppTheme" >
  6. <activity
  7. android:name=".MainActivity"
  8. android:label="@string/app_name" >
  9. <!-- <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter> -->
  13. </activity>
  14. <service android:name="com.phone.phonelistener.PhoneService"></service>
  15. <receiver android:name="com.phone.phonelistener.BootReceiver">
  16. <intent-filter >
  17. <action android:name="android.intent.action.BOOT_COMPLETED"/>
  18. </intent-filter>
  19. </receiver>
  20. </application>
4    使用服务注册特殊的广播接收者
    操作特别频繁的广播事件    比如屏幕的解锁和锁屏
    实现步骤
    [1]定义广播接收者    接收屏幕解锁屏幕的事件
  1. package com.phone.registerbroadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. public class ScreenReceiver extends BroadcastReceiver {
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. //获取当前事件类型
  9. String action = intent.getAction();
  10. if("android.intent.action.SCREEN_OFF".equals(action)){
  11. System.out.println("说明屏幕锁屏了");
  12. }else if("android.intent.action.SCREEN_ON".equals(action)){
  13. System.out.println("说明屏幕解屏了");
  14. }
  15. }
  16. }
    [2]写一个服务    用来注册广播接收者
  1. package com.phone.registerbroadcast;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.os.IBinder;
  6. public class ScreenService extends Service {
  7. private ScreenReceiver receiver;
  8. @Override
  9. public IBinder onBind(Intent intent) {
  10. // TODO Auto-generated method stub
  11. return null;
  12. }
  13. @Override
  14. public void onCreate() {
  15. //[1]创建ScreenReceiver实例
  16. receiver = new ScreenReceiver();
  17. //[2]获取IntentFilter 实例 目的是添加action
  18. IntentFilter filter = new IntentFilter();
  19. //[3]动态添加action
  20. filter.addAction("android.intent.action.SCREEN_OFF");
  21. filter.addAction("android.intent.action.SCREEN_ON");
  22. //[4]注册服务
  23. registerReceiver(receiver, filter);
  24. super.onCreate();
  25. }
  26. @Override
  27. public void onDestroy() {
  28. unregisterReceiver(receiver);
  29. super.onDestroy();
  30. }
  31. }
    [3]在MainActivity的onCreate方法中开启服务
  1. package com.phone.registerbroadcast;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. public class MainActivity extends Activity {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. Intent intent = new Intent(this,ScreenReceiver.class);
  11. startService(intent);
  12. }
  13. }
    [4]配置服务
  1. <service android:name="com.phone.registerbroadcast.ScreenService"></service>
5    bindService开启服务
    [1]第一次点击按钮 会执行服务的onCreate方法和onBind方法
    [2]当onBind方法返回null的时候onServiceConnected方法是不执行的
    [3]第二次点击按钮    服务没有响应
    [4]不求同时生 但求同时死    指的是调用者和服务之间
    [5]服务不可以多次解绑    多次解绑会报异常
    [6]通过bind方式开启服务    服务不能在设置页面找到    相当于是一个隐形的服务
6    为什么要引入bindService
    目的是为了调用服务里的方法
7    通过bindservice方式调用 服务方法里面的过程
    [1]在服务的内部定义一个方法    让activity调用
    [2]在服务的内容定义一个中间人对象(IBinder)
    [3]把定义的中间人对象在onBind方法里面返回
    [4]在mainactivity的oncreate方法里面调用bindservice的目的是为了获取我们定义的中间人对象
    [5]获取中间人对象
    [6]拿到中间人对象就可以间接调用到服务里面的方法
    [7]当Activity销毁的时候解绑服务
  1. package com.phone.ban;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.Binder;
  5. import android.os.IBinder;
  6. import android.widget.Toast;
  7. public class BanZhangService extends Service {
  8. //把定义的中间人对象返回
  9. @Override
  10. public IBinder onBind(Intent intent) {
  11. return new MyBinder();
  12. }
  13. public void banZhang(int money) {
  14. if(money > 1000){
  15. Toast.makeText(getApplicationContext(), "我是领导 把证给你办了", 1).show();
  16. }else{
  17. Toast.makeText(getApplicationContext(), "这点钱还想办事....", 1).show();
  18. }
  19. }
  20. //[1]定义中间人对象
  21. public class MyBinder extends Binder{
  22. public void callBanZheng(int money){
  23. //调用办证方法
  24. banZhang(money);
  25. }
  26. }
  27. }
  1. package com.phone.ban;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.Bundle;
  7. import android.os.IBinder;
  8. import android.view.View;
  9. import com.phone.ban.BanZhangService.MyBinder;
  10. public class MainActivity extends Activity {
  11. private MyConn conn;
  12. private MyBinder myBinder;//定义的中间人对象
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. Intent intent = new Intent(this,BanZhangService.class);
  18. //绑定服务
  19. conn = new MyConn();
  20. bindService(intent, conn, BIND_AUTO_CREATE);
  21. }
  22. //点击按钮调用服务里办证的方法
  23. public void click(View v){
  24. myBinder.callBanZheng(1001);
  25. }
  26. //监视服务的状态
  27. private class MyConn implements ServiceConnection{
  28. //当服务连接成功调用
  29. @Override
  30. public void onServiceConnected(ComponentName name, IBinder service) {
  31. myBinder = (MyBinder) service;
  32. }
  33. @Override
  34. public void onServiceDisconnected(ComponentName name) {
  35. }
  36. }
  37. @Override
  38. protected void onDestroy() {
  39. //当activity销毁的时候解绑服务
  40. unbindService(conn);
  41. super.onDestroy();
  42. }
  43. }
8    通过接口方式调用服务里面的方法
    接口可以隐藏代码内部的细节    让程序员暴露自己只想暴露的方法
    [1]定义一个接口    把想暴露的方法都定义在接口里面
    [2]我们定义的中间人对象事项我们自己定义的接口
    [3]在获取中间人对象的时候
  1. myBinder = (Iservice) service;
补充:混合方式开启服务
    需求:既想让服务在后台长期运行    又想调用服务里面的方法
    [1]先调用startService方法开启服务    能够保证服务在后台长期运行
    [2]调用bindService方法    去获取中间人对象
    [3]调用unbindSerivce    解绑服务
    [4]调用stopSerivce
9    百度音乐盒框架
  1. package com.phone.baidumusic;
  2. import android.content.ComponentName;
  3. import android.content.Intent;
  4. import android.content.ServiceConnection;
  5. import android.os.Bundle;
  6. import android.os.IBinder;
  7. import android.app.Activity;
  8. import android.view.View;
  9. public class MainActivity extends Activity {
  10. //中间人对象
  11. private IService iService;
  12. private MyConn conn;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. //混合方式开启服务
  18. //[1]先调用startservice 目的是可以保证服务在后台长期运行
  19. Intent intent = new Intent(this, MusicService.class);
  20. startService(intent);
  21. //[2]调用bindservice 目的为了获取我们定义的中间人对象 就可以间接的调用服务里面的方法了
  22. conn = new MyConn();
  23. bindService(intent, conn, BIND_AUTO_CREATE);
  24. }
  25. @Override
  26. protected void onDestroy() {
  27. //当Activity销毁的时候 解绑服务
  28. unbindService(conn);
  29. super.onDestroy();
  30. }
  31. //点击按钮 播放音乐
  32. public void click1(View v){
  33. iService.callPlayMusic();
  34. }
  35. //点击按钮 暂停音乐
  36. public void click2(View v){
  37. iService.callPauseMusic();
  38. }
  39. //点击按钮 继续播放音乐
  40. public void click3(View v){
  41. iService.callRePlayMusic();
  42. }
  43. //监听服务的状态
  44. private class MyConn implements ServiceConnection{
  45. @Override
  46. public void onServiceConnected(ComponentName name, IBinder service) {
  47. iService = (IService) service;
  48. }
  49. @Override
  50. public void onServiceDisconnected(ComponentName name) {
  51. }
  52. }
  53. }
  1. package com.phone.baidumusic;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.Binder;
  5. import android.os.IBinder;
  6. import android.view.View;
  7. public class MusicService extends Service {
  8. @Override
  9. public IBinder onBind(Intent intent) {
  10. return new MyBinder();
  11. }
  12. @Override
  13. public void onCreate() {
  14. super.onCreate();
  15. }
  16. @Override
  17. public void onDestroy() {
  18. super.onDestroy();
  19. }
  20. // 播放音乐
  21. public void playMusic() {
  22. // TODO 等讲完多媒体 再完善该案例
  23. System.out.println("音乐播放了");
  24. }
  25. // 暂停音乐
  26. public void pauseMusic() {
  27. // TODO 等讲完多媒体 再完善该案例
  28. System.out.println("音乐暂停了");
  29. }
  30. // 继续播放音乐
  31. public void rePlayMusic() {
  32. // TODO 等讲完多媒体 再完善该案例
  33. System.out.println("音乐继续播放了");
  34. }
  35. // [1]在服务的内部定义一个中间对象(IBinder)
  36. private class MyBinder extends Binder implements IService{
  37. @Override
  38. public void callPlayMusic() {
  39. playMusic();
  40. }
  41. @Override
  42. public void callPauseMusic() {
  43. pauseMusic();
  44. }
  45. @Override
  46. public void callRePlayMusic() {
  47. rePlayMusic();
  48. }
  49. }
  50. }
  1. package com.phone.baidumusic;
  2. public interface IService {
  3. public void callPlayMusic();
  4. public void callPauseMusic();
  5. public void callRePlayMusic();
  6. }
  1. <service android:name="com.phone.baidumusic.MusicService"></service>
10    aidl介绍
    本地服务:运行自己应用里面的服务
    远程服务:运行在其他应用里面的服务
    实现进程间通信    IPC
    aidl:Android Interface defined Language    安卓接口定义语言
            专门是用来解决进程间通信的
    
    总结:使用aidl的步骤
   [1]把IService.java文件变成一个aidl文件
   [2]adil这个语言不认识public 把public去掉
   [3]自动生成一个IService.java文件 系统自动帮助我们生成一个类Stub(在gen目录下)
   [4]我们自己定义的中间人对象直接继承Stub
   [5]保证2应用的aidl文件是同一个    保证aidl文件所在的包名相同
   [6]获取中间人对象的方式不一样了
    IService  = Stub.asInterface(service);
11    aidl的应用场景
    比如    支付宝

day08 服务的更多相关文章

  1. day08.4-samba共享网盘服务

    1. 安装软件:yum   install   samba   -y 2. 新建共享目录物理路径:mkdir   /zizaijiapu 修改配置文件:vim   /etc/samba/smb.con ...

  2. day08.3-apache网页服务

    1. 安装软件:yum   install   httpd   -y 2. 查看配置文件:vim   /etc/httpd/conf/httpd.cof,其中,"Listen   80&qu ...

  3. day08.2-ssh远程连接服务

    在Linux环境中,部署一个服务的一般步骤: a). 准备环境,包括 关闭防火墙:service   iptables   stop(或chkconfig   iptables   off) 关闭se ...

  4. Python面向对象进阶和socket网络编程-day08

    写在前面 上课第八天,打卡: 为什么坚持?想一想当初: 一.面向对象进阶 - 1.反射补充 - 通过字符串去操作一个对象的属性,称之为反射: - 示例1: class Chinese: def __i ...

  5. HTTP协议基础与web服务的重定向,跳转以及请求转发

    JavaWeb中,HttpServletRequest与HttpServletResponse几乎是处理各种请求与操作必备的参数,与原始的ServletRequest/ServletResponse相 ...

  6. day08:软件系统的体系结构&Tomcat详解&Web应用&http协议

        day08 软件系统体系结构     常见软件系统体系结构B/S.C/S 1.1 C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端 ...

  7. python开发学习-day08(socket高级、socketserver、进程、线程)

    s12-20160305-day08 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  8. Dubbo学习系列之六(微服务架构实战)

    看了最近文章的反馈,似乎波澜不惊的样子,应该是看官觉得都是小菜,那我就直上硬菜,人狠话不多,开始!准备:Idea201902/JDK11/ZK3.5.5/Gradle5.4.1/RabbitMQ3.7 ...

  9. 阶段5 3.微服务项目【学成在线】_day08 课程图片管理 分布式文件系统_06-分布式文件系统研究-fastDFS安装及配置文件说明

    3 fastDFS入门 3.1fastDFS安装与配置 3.1.1 导入虚拟机 对fastDFS的安装过程不要求学生掌握,可以直接导入老师提供虚拟机. 1.使用Vmware打开虚拟机配置文件“Cent ...

随机推荐

  1. linux 使用ifstat查看网络使用情况

    首先安装ifstat wget http://distfiles.macports.org/ifstat/ifstat-1.1.tar.gz tar xzvf ifstat-1.1.tar.gz cd ...

  2. angularjs 1.x lazyloading

    https://oclazyload.readme.io/docs 安装好后直接使用 var myApp = angular.module("MyApp", ["oc.l ...

  3. 我的第一个HTML5应用

    直接贴代码: 源代码: <?xml version="1.0" encoding="UTF-8"?> <div xmlns="htt ...

  4. 基于Ubuntu 搭建 VNC 远程桌面服务

    系统要求:Ubuntu 16.04.1 LTS 64 位操作系统 安装.启动 VNC VNC 远程桌面原理 注:本小节内容旨在帮助您更好地了解 Xorg.X11.VNC 等概念和原理,如果你不想了解原 ...

  5. Xilinx 常用模块汇总(verilog)【03】

    作者:桂. 时间:2018-05-10  2018-05-10  21:03:44 链接:http://www.cnblogs.com/xingshansi/p/9021919.html 前言 主要记 ...

  6. Lambert漫反射的BRDF

    Lambert漫反射brdf=Albedo/pi. 推导: 如图,设Lambert面元获得入射照度Ei,各方向均匀释放亮度,每个方向释放的亮度都是Lo. 又设此表面反射率为Albedo,根据反射率定义 ...

  7. linux每日命令(6):rm命令

    rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有文件均保持不变. rm是一个危险的命令,使用的 ...

  8. vuex的理解

    首先需要了解vuex的基本概念和使用方式,vue的官网也有很详细的说明或者浏览:https://zhuanlan.zhihu.com/p/24357762. vue是单页应用所以当页面刷新时vuex的 ...

  9. 教你怎么上传本地代码到github

    第一步:建立git仓库 cd到你的本地项目根目录下,执行git命令 git init 第二步:去github上创建自己的Repository,创建完成后拿到创建的仓库的https地址 第三步:将本地的 ...

  10. WebRTC 零基础开发者教程(中文版)下载

    WebRTC 简介 WebRTC,是一个支持网页浏览器进行实时语音通话或视频聊天的技术,是谷歌2010年以6820万美元收购Global IP Solutions公司而获得的一项技术. WebRTC提 ...