惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
F
Fortinet All Blogs
U
Unit 42
F
Full Disclosure
雷峰网
雷峰网
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
罗磊的独立博客
D
DataBreaches.Net
C
Check Point Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
O
OpenAI News
C
CXSECURITY Database RSS Feed - CXSecurity.com
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Hacker News
The Hacker News
Webroot Blog
Webroot Blog
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
N
News | PayPal Newsroom
P
Proofpoint News Feed
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
Google Online Security Blog
Google Online Security Blog
H
Help Net Security
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
M
MIT News - Artificial intelligence
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
MyScale Blog
MyScale Blog
腾讯CDC

博客园 - 恭喜发财

(转)Xml DML - 恭喜发财 - 博客园 (转)sql:variable() binding and modify method of xquery: insert XML DML 在WinForm中控制GIF动画的启停的一种方法(转) SQL Server Backup file in standard Zip format Working with binary large objects (BLOBs) c# 得到局域网中可用SqlServer服务器列表(转) C#实现UDP协议(转) 健康大讲堂—凡膳皆药 寓医于食 ReportView(RDSL)参考资料 How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6) 汇总c#.net常用函数和方法集 在多线程中如何调用Winform 使用C#在进度条中显示复制文件的进度(转) c#将大文件读取或写入到数据库(带进度条的源码)(转) 写C#自定义控件的心得 Finalize(析构函数)、Dispose、Close 的区别与使用 如何在WinForm中对DataGrid进行分页显示(转) 如何用C#做一个类似于桌面插件的程序(转) c#连接各类数据库大全
android 电话状态的监听(来电和去电)
恭喜发财 · 2012-08-15 · via 博客园 - 恭喜发财

实现手机电话状态的监听,主要依靠两个类:TelephoneManger和PhoneStateListener。
TelephonseManger提供了取得手机基本服务的信息的一种方式。因此应用程序可以使用TelephonyManager来探测手机基本服务的情况。应用程序可以注册listener来监听电话状态的改变。我们不能对TelephonyManager进行实例化,只能通过获取服务的形式:
Context.getSystemService(Context.TELEPHONY_SERVICE);
注意:对手机的某些信息进行读取是需要一定许可(permission)的。
 
主要静态成员常量:(它们对应PhoneStateListener.LISTEN_CALL_STATE所监听到的内容)
int CALL_STATE_IDLE   空闲状态,没有任何活动。
int CALL_STATE_OFFHOOK  摘机状态,至少有个电话活动。该活动或是拨打(dialing)或是通话,或是 on hold。并且没有电话是ringing or waiting
int CALL_STATE_RINGING  来电状态,电话铃声响起的那段时间或正在通话又来新电,新来电话不得不等待的那段时间。
 
手机通话状态在广播中的对应值
 
EXTRA_STATE_IDLE 它在手机通话状态改变的广播中,用于表示CALL_STATE_IDLE状态
EXTRA_STATE_OFFHOOK 它在手机通话状态改变的广播中,用于表示CALL_STATE_OFFHOOK状态
EXTRA_STATE_RINGING 它在手机通话状态改变的广播中,用于表示CALL_STATE_RINGING状态
ACTION_PHONE_STATE_CHANGED 在广播中用ACTION_PHONE_STATE_CHANGED这个Action来标示通话状态改变的广播(intent)。
注:需要许可READ_PHONE_STATE。
String EXTRA_INCOMING_NUMBER 
在手机通话状态改变的广播,用于从extra取来电号码。
String EXTRA_STATE  在通话状态改变的广播,用于从extra取来通话状态。
 
主要成员函数
public int getCallState() 取得手机的通话状态。
public CellLocation getCellLocation () 返回手机当前所处的位置。如果当前定位服务不可用,则返回null
注:需要许可(Permission)ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
public int getDataActivity () 返回当前数据连接活动状态的情况。
public int getDataState () 返回当前数据连接状态的情况。
public String getDeviceId ()
返回手机的设备ID。比如对于GSM的手机来说是IMEI码,对于CDMA的手机来说MEID码或ESN码。如果读取失败,则返回null。
 
如何实现电话状态的监听呢?
 
Android在电话状态改变是会发送action为android.intent.action.PHONE_STATE的广播,而拨打电话时会发送action为android.intent.action.NEW_OUTGOING_CALL的广播,但是我看了下开发文档,暂时没发现有来电时的广播。通过自定义广播接收器,接受上述两个广播便可。
 
Java代码:
package com.pocketdigi.phonelistener;
 
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
 
public class PhoneReceiver extends BroadcastReceiver {
 
 @Override
 public void onReceive(Context context, Intent intent) {
  System.out.println("action"+intent.getAction());
  //如果是去电
  if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
     String phoneNumber = intent
     .getStringExtra(Intent.EXTRA_PHONE_NUMBER);
     Log.d(TAG, "call OUT:" + phoneNumber); 
   }else{
   //查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电.
   //如果我们想要监听电话的拨打状况,需要这么几步 :
    * 第一:获取电话服务管理器TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE);
    * 第二:通过TelephonyManager注册我们要监听的电话状态改变事件。manager.listen(new MyPhoneStateListener(),
    * PhoneStateListener.LISTEN_CALL_STATE);这里的PhoneStateListener.LISTEN_CALL_STATE就是我们想要
    * 监听的状态改变事件,初次之外,还有很多其他事件哦。
    * 第三步:通过extends PhoneStateListener来定制自己的规则。将其对象传递给第二步作为参数。
    * 第四步:这一步很重要,那就是给应用添加权限。android.permission.READ_PHONE_STATE


   TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); 
   tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
   //设置一个监听器
  }
 }
 PhoneStateListener listener=new PhoneStateListener(){
 
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
   //注意,方法必须写在super方法后面,否则incomingNumber无法获取到值。
   super.onCallStateChanged(state, incomingNumber);
   switch(state){
   case TelephonyManager.CALL_STATE_IDLE:
    System.out.println("挂断");
    break;
   case TelephonyManager.CALL_STATE_OFFHOOK:
    System.out.println("接听");
    break;
   case TelephonyManager.CALL_STATE_RINGING:
    System.out.println("响铃:来电号码"+incomingNumber);
    //输出来电号码
    break;
   }
  }
 };
}
要在AndroidManifest.xml注册广播接收器:
<receiver android:name=".PhoneReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.PHONE_STATE"/>  
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />  
        </intent-filter>  
       </receiver>  
 <receiver android:name=".PhoneReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver>

还要添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>  
   <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>