[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>

posted @ 2016-03-28 23:32  唯一客服系统开发笔记  阅读(2630)  评论(0编辑  收藏  举报