后台服务运行示例

监听电话状态的Android应用
本文介绍了一个简单的Android应用程序,该程序通过监听电话状态来识别来电并作出响应。主要涉及MainActivity和PhoneStatusService两个类的编写,以及在AndroidManifest.xml中进行必要的配置。

1、编写MainActivity.java类

package com.example.callstatuslintener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //开启服务
        Intent intent=new Intent(this, PhoneStatusService.class);
        startService(intent);
    }

}
View Code

2、编写PhoneStatusService.java类

/**
 * 
 */
package com.example.callstatuslintener;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

/**
 * 长期后台运行的组件,如果用户不手动关闭,不会停止的
 *
 * 2013-12-6
 */
public class PhoneStatusService extends Service {

    /* (non-Javadoc)
     * @see android.app.Service#onBind(android.content.Intent)
     */
    @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("服务被创建了");
        //监视用户电话状态的变化
        //电话管理器 电话管理服务
        TelephonyManager tm=(TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        //监听手机的通话状态的变化
        tm.listen(new MyPhoneStatusLinstener(), PhoneStateListener.LISTEN_CALL_STATE);
    }
    private class MyPhoneStatusLinstener extends PhoneStateListener{

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE: //空闲状态,没有通话没有响铃
                
                break;
            case TelephonyManager.CALL_STATE_RINGING:    //响铃状态
                System.out.println("发现来电号码:"+incomingNumber);
                if ("5566".equals(incomingNumber)) {
                    System.out.println("挂断电话");
                }
                
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:    //通话状态
                break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
        
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("服务被销毁了……");
    }
    
}
View Code

3、注册服务AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.callstatuslintener"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <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="com.example.callstatuslintener.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="com.example.callstatuslintener.PhoneStatusService"></service>
    </application>

</manifest>
View Code

 demo下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值