要監聽android打電話和接電話,有一種的是經過新建一個Receiver繼承自BroadcastReceiver。java
還有一種也可經過PhoneStateListener來實現。今天就說說後面一種,廢話不說了,直接上代碼android
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package
com.webphone;
import
org.apache.http.message.BasicNameValuePair;
import
android.content.Context;
import
android.content.Intent;
import
android.graphics.PixelFormat;
import
android.telephony.PhoneStateListener;
import
android.telephony.TelephonyManager;
import
android.view.WindowManager;
import
android.widget.TextView;
public
class
TelListener
extends
PhoneStateListener {
private
Context context;
private
WindowManager wm;
//系統管理
private
TextView tv;
//懸浮窗文本
private
Boolean isInComing=
false
;
//標記是否爲來電
public
TelListener(Context context){
this
.context = context;
}
@Override
public
void
onCallStateChanged(
int
state, String incomingNumber) {
switch
(state)
{
case
TelephonyManager.CALL_STATE_RINGING:
//此時爲響鈴狀態
{
isInComing=
true
;
//若是有響鈴狀態,則表示此時爲來電
wm = (WindowManager)context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
tv =
new
TextView(context);
String comingNum= GetInComingNumber();
tv.setTextSize(
40
);
tv.setText(
"來電"
+comingNum);
wm.addView(tv, params);
//添加到系統窗口中。
}
break
;
case
TelephonyManager.CALL_STATE_OFFHOOK:
//若是處於忙時狀態
{
if
(!isInComing)
//若是非來電狀態,則標記爲去電
{
if
(wm !=
null
)
wm.removeView(tv);
wm = (WindowManager)context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params =
new
WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
tv =
new
TextView(context);
tv.setTextSize(
40
);
//獲取實際去電號碼,此處使用自定義方法,(這邊去電號碼本身想辦法保存)
String outcomingNum=AppData.getRealNum();
tv.setText(
"去電"
+ outcomingNum);
wm.addView(tv, params);
isInComing=
false
;
//標記爲非來電
}
}
break
;
case
TelephonyManager.CALL_STATE_IDLE:
{
if
(wm !=
null
&& tv!=
null
)
wm.removeView(tv);
}
break
;
}
super
.onCallStateChanged(state, incomingNumber);
}
}
|
固然了,雖然代碼是OK了,可是記得配置好權限。
1
|
<
uses-permission
android:name
=
"android.permission.READ_PHONE_STATE"
></
uses-permission
>
|
雖然如此,既然是彈出懸浮窗,那也該配置下彈出懸浮窗的權限吧。
1
|
<
uses-permission
android:name
=
"android.permission.SYSTEM_ALERT_WINDOW"
/>
|
都好了,那最重要的一步可別忘記了,在啓動的時候添加監聽,而且註冊廣播。
1
2
3
|
TelephonyManager telM = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//設置監聽
telM.listen(
new
TelListener(
this
.getContext()), PhoneStateListener.LISTEN_CALL_STATE);
|
還要在AndroidManifest.xml中註冊廣播接收類
1
2
3
4
5
|
<
receiver
android:name
=
".PhoneBootReceiver"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.BOOT_COMPLETED"
/>
</
intent-filter
>
</
receiver
>
|
若是想要接收開機廣播,而後在接收到廣播後註冊來電監聽。接收開機廣播須要有「RECEIVE_BOOT_COMPLETED」權限,AndroidManifest.xml 中申明以下
1
|
<
uses-permission
android:name
=
"android.permission.RECEIVE_BOOT_COMPLETED"
/>
|
其實android API自己是沒法監聽去電的,這只是一種轉彎的方法。。
PS:在轉個彎,你就能夠實現來電歸屬地查詢了。。
本文從百度空間搬家到博客園。。