22、e/ic_launcher”
10. android:label="string/app_name" >
11. 〈activity
12. android:name=”、MainActivity"
13. android:label=”string/app_name" 〉
14.
16.
23、 <category android:name="android、intent、category、LAUNCHER" />
17. 〈/intent-filter>
18. activity〉
19.
20. 〈/manifest>
④PhoneStateListener 例子
更新上面得Activity,模拟拨打电话得状态,当电话拨打结束后,返回原始Activity,实际上只就是重启了这个activity。
1. package net、cublog、android;
24、2. import android、app、Activity;
3. import android、content、Context;
4. import android、content、Intent;
5. import android、net、Uri;
6. import android、os、Bundle;
7. import android、telephony、PhoneStateListener;
8. import android、telephony、TelephonyManager;
9. import android、util、Log;
10. import an
25、droid、view、View;
11. import android、view、View、OnClickListener;
12. import android、widget、Button;
13. public class MainActivity extends Activity {
14. private Button button;
15.
16. Override
17. public void onCreate(Bundle savedInstanceState) {
18.
19. super、onCre
26、ate(savedInstanceState);
20. setContentView(R、layout、main);
21.
22. button = (Button) findViewById(R、id、buttonCall);
23.
24. // add PhoneStateListener
25. PhoneCallListener phoneListener = new PhoneCallListener();
26. TelephonyManager telepho
27、nyManager = (TelephonyManager) this
27. 、getSystemService(Context、TELEPHONY_SERVICE);
28. telephonyManager、listen(phoneListener,PhoneStateListener、LISTEN_CALL_STATE);
29.
30.
31. // add button listener
32. button、setOnClickListener(new OnClickList
28、ener() {
33.
34. Override
35. public void onClick(View arg0) {
36.
37. Intent callIntent = new Intent(Intent、ACTION_CALL);
38. callIntent、setData(Uri、parse("tel:"));
39. startActivity(callIntent);
40.
41.
29、 }
42.
43. });
44.
45. }
46.
47. //monitor phone call activities
48. private class PhoneCallListener extends PhoneStateListener {
49.
50. private boolean isPhoneCalling = false;
51.
52. String LOG_TAG = "LOGGING 123";
53.
54.
30、 Override
55. public void onCallStateChanged(int state, String iningNumber) {
56.
57. if (TelephonyManager、CALL_STATE_RINGING == state) {
58. // phone ringing
59. Log、i(LOG_TAG, "RINGING, number: ” + iningNumber);
60. }
61.
31、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.
32、 // run when class initial and phone call ended,
71. // need detect flag from CALL_STATE_OFFHOOK
72. Log、i(LOG_TAG, "IDLE");
73.
74. if (isPhoneCalling) {
75.
76. Log、i(LOG_TAG, "restart app");
77.
78.
33、 // restart app
79. Intent i = getBaseContext()、getPackageManager()
80. 、getLaunchIntentForPackage(
81. getBaseContext()、getPackageName());
82. i、addFlags(Intent、FLAG_ACTIVITY_CLEAR_TOP);
8
34、3. startActivity(i);
84.
85. isPhoneCalling = false;
86. }
87.
88. }
89. }
90. }
91. }
由于PhoneStateListener需要READ_PHONE_STATE权限,需要在AndroidManifest、xml中添加以下代码:
1. 〈uses—permission android:name="android、permi
35、ssion、READ_PHONE_STATE" /〉
添加后AndroidManifest、xml如下:
1. 〈?xml version="1、0” encoding="utf—8"?>
2. <manifest xmlns:android=""
3. package="net、cublog、android"
4. android:versionCode="1"
5. android:versionName="1、0” >
6.