zoukankan      html  css  js  c++  java
  • [android] 采用服务执行长期后台的操作

    服务:在后台长期运行的没有界面的组件

    新建一个类PhoneService类,继承系统的Service

    清单文件中 进行配置

    新建一个节点<service>,设置名称android:name=”.PhoneService”

    类里面有几个重要方法

    onCreate()方法,服务被创建的时候调用

    onDestory()方法,服务被销毁的时候调用

    开启服务

    获取intent对象,new Intent(this,PhoneService.class),参数:上下文,字节码

    调用上下文对象的startService(intent),参数:intent对象

    在服务的onCreate()方法里,执行一些长期操作

    获取TelephoneyManager对象,调用getSystemService(TELEPHONY_SERVICE)方法

    调用TelephoneyManager对象的listen(istenerevents)方法,监听手机通话状态,参数:

    PhoneStateListener对象,使用内部类类继承一下,要重写一些方法

    PhoneStateListener.LISTEN_CALL_STATE

    新建一个内部类MyPhoneStateListener继承PhoneStateListener

    重写方法onCallStateChanged(state,incomingNumber),当手机的电话状态变化的时候,回调此函数

    在上面方法里面,switch判断一下通话状态,有以下三种TelephonyManager.CALL_STATE_IDLE空闲状态,TelephonyManager.CALL_STATE_RINGING响铃状态,

    TelephonyManager.CALL_STATE_OFFHOOK通话状态

    需要权限android.permission.READ_PHONE_STATE

    MainActivity.java

    package com.tsh.listentel;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //开启服务
            Intent intent=new Intent(this,PhoneService.class);
            startService(intent);
        }
    }

    PhoneService.java

    package com.tsh.listentel;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    
    public class PhoneService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
        //服务创建
        @Override
        public void onCreate() {
            super.onCreate();
            System.out.println("服务创建");
            TelephonyManager tm=(TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    
            tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
        }
        //内部类
        private class MyPhoneStateListener extends PhoneStateListener{
    
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    System.out.println("空闲状态");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    System.out.println("响铃状态");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    System.out.println("通话状态");
                    break;
                default:
                    break;
                }
            }
            
        }
        //服务销毁
        @Override
        public void onDestroy() {
            System.out.println("服务销毁");
            super.onDestroy();
        }
    
    }

    Manifest.xml 

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.tsh.listentel"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="23" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <service android:name=".PhoneService"></service>
        </application>
    
    </manifest>

  • 相关阅读:
    阿里巴巴图标库在项目中的用法
    js对象的深拷贝
    Ajax
    HTML5新增的canvas是什么--通过刮奖效果学习
    一些最基础的面试题
    微信小程序实现列表搜索功能
    vue的基础双向绑定
    ES6 Promise 的不完全实现
    JQ学习
    播放音乐进度条
  • 原文地址:https://www.cnblogs.com/taoshihan/p/5331232.html
Copyright © 2011-2022 走看看