在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) {
if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { // 去电
String outgoingNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER); // 去电号码
Intent pit = new Intent(context, PhoneService.class);
pit.putExtra("outgoingNumber", outgoingNumber);
context.startService(pit);
} else { // 来电
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;
private String outgoingNumber = null ;
@Override
public void onCreate() { // 服务创建的时候操作
super.onCreate();
this.telephony = (TelephonyManager) super
.getSystemService(Context.TELEPHONY_SERVICE);
this.telephony.listen(new PhoneStateListenerImpl(),
PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作
}
@Override
public void onStart(Intent intent, int startId) {
this.outgoingNumber = intent.getStringExtra("outgoingNumber") ;
super.onStart(intent, startId);
}
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: // 接听电话
System.out.println("拨出电话号码:"
+ PhoneService.this.outgoingNumber
+ ",拨出时间:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date()));
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" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<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.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>