name="android、permission、CALL_PHONE"/>
8. 9. android:
icon="@drawable/ic_launcher"
10. android:
label="@string/app_name">
11. 12. android:
name="、MainActivity"
13. android:
label="@string/app_name">
14.
15. name="android、intent、action、MAIN"/>
16. name="android、intent、category、LAUNCHER"/>
17.
18.
19.
20.
④PhoneStateListener例子
更新上面的Activity,模拟拨打电话的状态,当电话拨打结束后,返回原始Activity,实际上只就是重启了这个activity。
1.packagenet、cublog、android;
2.importandroid、app、Activity;
3.importandroid、content、Context;
4.importandroid、content、Intent;
5.importandroid、net、Uri;
6.importandroid、os、Bundle;
7.importandroid、telephony、PhoneStateListener;
8.importandroid、telephony、TelephonyManager;
9.importandroid、util、Log;
10.importandroid、view、View;
11.importandroid、view、View、OnClickListener;
12.importandroid、widget、Button;
13.publicclassMainActivityextendsActivity{
14. privateButtonbutton;
15.
16. @Override
17. publicvoidonCreate(BundlesavedInstanceState){
18.
19. super、onCreate(savedInstanceState);
20. setContentView(R、layout、main);
21.
22. button=(Button)findViewById(R、id、buttonCall);
23.
24. //addPhoneStateListener
25. PhoneCallListenerphoneListener=newPhoneCallListener();
26. TelephonyManagertelephonyManager=(TelephonyManager)this
27. 、getSystemService(Context、TELEPHONY_SERVICE);
28. telephonyManager、listen(phoneListener,PhoneStateListener、LISTEN_CALL_STATE);
29.
30.
31. //addbuttonlistener
32. button、setOnClickListener(newOnClickListener(){
33.
34. @Override
35. publicvoidonClick(Viewarg0){
36.
37. IntentcallIntent=newIntent(Intent、ACTION_CALL);
38. callIntent、setData(Uri、parse("tel:
"));
39. startActivity(callIntent);
40.
41. }
42.
43. });
44.
45. }
46.
47. //monitorphonecallactivities
48. privateclassPhoneCallListenerextendsPhoneStateListener{
49.
50. privatebooleanisPhoneCalling=false;
51.
52. StringLOG_TAG="LOGGING123";
53.
54. @Override
55. publicvoidonCallStateChanged(intstate,StringincomingNumber){
56.
57. if(TelephonyManager、CALL_STATE_RINGING==state){
58. //phoneringing
59. Log、i(LOG_TAG,"RINGING,number:
"+incomingNumber);
60. }
61.
62. if(TelephonyManager、CALL_STATE_OFFHOOK==state){
63. //active
64. Log、i(LOG_TAG,"OFFHOOK");
65.
66. isPhoneCalling=true;
67. }
68.
69. if(TelephonyManager、CALL_STATE_IDLE==state){
70. //runwhenclassinitialandphonecallended,
71. //needdetectflagfromCALL_STATE_OFFHOOK
72. Log、i(LOG_TAG,"IDLE");
73.
74. if(isPhoneCalling){
75.
76. Log、i(LOG_TAG,"restartapp");
77.
78. //restartapp
79. Intenti=getBaseContext()、getPackageManager()
80. 、getLaunchIntentForPackage(
81. getBaseContext()、getPackageName());
82. i、addFlags(Intent、FLAG_ACTIVITY_CLEAR_TOP);
83. startActivity(i);
84.
85. isPhoneCalling=false;
86. }
87.
88. }
89. }
90. }
91.}
由于PhoneStateListener需要READ_PHONE_STATE权限,需要在AndroidManifest、xml中添加以下代码:
1.name="android、permission、READ_PHONE_STATE"/>
添加后Andr