读书人

复电监听

发布时间: 2013-10-08 16:55:16 作者: rapoo

来电监听

复电监听

复电监听

在main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#00ff66">

</LinearLayout>

在PhoneBroadcastReceiver.java中:

package com.li.phone;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

public class PhoneBroadcastReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

context.startService(new Intent(context, PhoneService.class));

}

}

在PhoneService.java中:

package com.li.phone;

import java.text.SimpleDateFormat;

import java.util.Date;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.os.IBinder;

import android.telephony.PhoneStateListener;

import android.telephony.TelephonyManager;

public class PhoneService extends Service {

private TelephonyManager telephony = null;

@Override

public void onCreate() { // 服务创建的时候操作

super.onCreate();

this.telephony = (TelephonyManager) super

.getSystemService(Context.TELEPHONY_SERVICE);

this.telephony.listen(new PhoneStateListenerImpl(),

PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作

}

private class PhoneStateListenerImpl 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

+ ",拨入时间:"

+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

.format(new Date()));

break;

case TelephonyManager.CALL_STATE_OFFHOOK: // 接听电话

break;

}

}

}

@Override

public IBinder onBind(Intent intent) {

return null;

}

}

在MyPhoneDemo.java中:

package com.li.phone;

import android.app.Activity;

import android.os.Bundle;

public class MyPhoneDemo extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

修改AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.li.phone"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".MyPhoneDemo"

android:label="@string/title_activity_my_phone_demo" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service android:name=".PhoneService" />

<receiver android:name=".PhoneBroadcastReceiver">

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED" />

<action android:name="android.intent.action.PHONE_STATE" />

</intent-filter>

</receiver>

</application>

</manifest>

读书人网 >移动开发

热点推荐