来去电监听

 



 

 

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值