无法捕获TelephonyManager.EXTRA_STATE_RINGING

写一个最简单的捕获电话状态的功能,但是在模拟器上测试通过,真机上只会进入idle

  1 package com.example.contextawaredemo;
  2 
  3 import android.app.Activity;
  4 import android.app.Service;
  5 import android.content.BroadcastReceiver;
  6 import android.content.Context;
  7 import android.content.Intent;
  8 import android.content.IntentFilter;
  9 import android.os.Bundle;
 10 import android.telephony.PhoneStateListener;
 11 import android.telephony.TelephonyManager;
 12 import android.widget.Toast;
 13 
 14 
 15 public class PhoneStatReceiver {
 16     private static final String TAG = "PhoneStateReceiver";
 17     private static final String PHONE_STATE = "android.intent.action.PHONE_STATE";
 18     private static final String OUTGOING_CALL = "android.intent.action.NEW_OUTGOING_CALL";
 19     private static boolean incomingFlag = false;
 20     BroadcastReceiver callReceiver;
 21     SimplePost post=new SimplePost();
 22     static boolean addReceiver=false;
 23     
 24     
 25     
 26     public void addReceiver(Activity act){
 27         addReceiver=true;
 28         
 29 callReceiver = new BroadcastReceiver(){
 30             
 31             @Override
 32             public void onReceive(Context context, Intent intent){
 33                 //
 34                 System.out.println("in the onReceive funchion now ~~");
 35                 System.out.println("flag is: "+ incomingFlag);
 36                 if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
 37                     incomingFlag = true;
 38                     System.out.print("call out");
 39                     System.out.println("in the if flag is: "+ incomingFlag);
 40                     post.sendMessage("calling");
 41                 }else{
 42                     //TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
 43                     System.out.println("in the else flag is: "+ incomingFlag);
 44                     
 45                     String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
 46                     System.out.println("state is:"+ state);
 47                     if(TelephonyManager.EXTRA_STATE_RINGING.equals(state)){
 48                         System.out.println("ringing");
 49                         incomingFlag=true;
 50                         System.out.println("a new call coming in");
 51                         Toast.makeText(context, "new call come in", Toast.LENGTH_LONG).show();
 52                         //calling
 53                         post.sendMessage("calling");
 54                     }
 55                     if(TelephonyManager.EXTRA_STATE_IDLE.equals(state)){
 56                         System.out.println("idle");
 57                         if(incomingFlag){
 58                             System.out.println("idle,not active now");
 59                             Toast.makeText(context, "idle", Toast.LENGTH_LONG).show();
 60                             //finished
 61                             post.sendMessage("finished");
 62                             }
 63                             incomingFlag = false;
 64                     }
 65                     if(TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)){
 66                         System.out.println("OFF HOOK");
 67                         if(incomingFlag){
 68                             System.out.println("offhook");
 69                             //in call
 70                             post.sendMessage("inCall");
 71                             }
 72                         
 73                     }
 74                     
 75                 }
 76             }
 77                     
 78                     
 79 //                    switch (tm.getCallState()){
 80 //                    case TelephonyManager.CALL_STATE_RINGING:
 81 //                        incomingFlag=true;
 82 //                        System.out.println("a new call coming in");
 83 //                        Toast.makeText(context, "new call come in", Toast.LENGTH_LONG).show();
 84 //                        //calling
 85 //                        post.sendMessage("calling");
 86 //                        break;
 87 //                    case TelephonyManager.CALL_STATE_OFFHOOK:
 88 //                        System.out.println("out of offhook");
 89 //                        if(incomingFlag){
 90 //                        System.out.println("offhook");
 91 //                        //in call
 92 //                        post.sendMessage("inCall");
 93 //                        }
 94 //                        break;
 95 //                    case TelephonyManager.CALL_STATE_IDLE:
 96 //                        System.out.println("out of idle");
 97 //                        //just for test local - idle
 98 //                        if(incomingFlag){
 99 //                        System.out.println("idle,not active now");
100 //                        Toast.makeText(context, "idle", Toast.LENGTH_LONG).show();
101 //                        //finished
102 //                        post.sendMessage("finished");
103 //                        }
104 //                        incomingFlag = false;
105 //                        break;
106 //                    default:
107 //                        System.out.println("do nothing");
108 //                            
109 //                        
110 //                        
111 //                    }
112                     
113                     //TEST THE LISTENER
114 //                    tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
115 //                    
116 //                    
117 //                }
118 //                
119 //                
120 //            }
121 //            PhoneStateListener listener=new PhoneStateListener(){
122 //            @Override
123 //            public void onCallStateChanged(int state,String incomingNumber){
124 //                super.onCallStateChanged(state, incomingNumber);
125 //                switch(state){
126 //                
127 //                case TelephonyManager.CALL_STATE_RINGING:
128 //                    System.out.println("in the switch call state ringing flag is: "+ incomingFlag);
129 //                    incomingFlag=true;
130 //                    System.out.println("a new call coming in");
131 //                    //Toast.makeText(context, "new call come in", Toast.LENGTH_LONG).show();
132 //                    //calling
133 //                    post.sendMessage("calling");
134 //                    break;
135 //                case TelephonyManager.CALL_STATE_OFFHOOK:
136 //                    System.out.println("in the switch call state offhook flag is: "+ incomingFlag);
137 //                    System.out.println("out of offhook");
138 //                    if(incomingFlag){
139 //                    System.out.println("offhook");
140 //                    //in call
141 //                    post.sendMessage("inCall");
142 //                    }
143 //                    break;
144 //                case TelephonyManager.CALL_STATE_IDLE:
145 //                    System.out.println("in the switch call state idel flag is: "+ incomingFlag);
146 //                    System.out.println("out of idle");
147 //                    //just for test local - idle
148 //                    if(incomingFlag){
149 //                    System.out.println("idle,not active now");
150 //                    //Toast.makeText(context, "idle", Toast.LENGTH_LONG).show();
151 //                    //finished
152 //                    post.sendMessage("finished");
153 //                    }
154 //                    //incomingFlag = false;
155 //                    break;
156 //                    
157 //                }
158 //                
159 //            }};
160             };
161         
162         System.out.println("add receiver in phoneStatReceiver");
163         IntentFilter filter = new IntentFilter();
164         filter.addAction(PHONE_STATE);
165         filter.addAction(OUTGOING_CALL);
166         if(this.callReceiver==null){
167             System.out.println("call receiver is null");
168         }
169         
170         act.registerReceiver(callReceiver,filter);
171 //        registerReceiver(callReceiver, new IntentFilter(PHONE_STATE));
172 //        registerReceiver(callReceiver, new IntentFilter(OUTGOING_CALL));
173         System.out.println("add receiver finished");
174         
175         //test whether add success
176 //        sendBroadcast(new Intent(PHONE_STATE));
177 //        System.out.println("test out going call");
178 //        sendBroadcast(new Intent(OUTGOING_CALL));
179     }
180 
181 
182     public void DestroyReceiver(Activity act){
183         if(addReceiver){
184         act.unregisterReceiver(callReceiver);
185         System.out.println("destroy receiver finished");}
186         else{
187             System.out.println("nothing to destroy");
188         }
189     }
190 }

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.contextawaredemo"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
11     <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
12     <uses-permission android:name="android.permission.CALL_PHONE"/>
13    
14         
15     <uses-permission android:name="android.permission.INTERNET"/>
16 
17     <application
18         android:allowBackup="true"
19         android:icon="@drawable/ic_launcher"
20         android:label="@string/app_name"
21         android:theme="@style/AppTheme" >
22 <!--         <receiver android:name="PhoneStatReceiver"> -->
23 <!--             <intent-filter> -->
24 <!--                 <action android:name="android.intent.action.PHONE_STATE"/> -->
25 <!--                 <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> -->
26 <!--             </intent-filter> -->
27 <!--         </receiver> -->
28         <activity
29             android:name="com.example.contextawaredemo.MainActivity"
30             android:label="@string/app_name" >
31             <intent-filter>
32                 <action android:name="android.intent.action.MAIN" />
33 
34                 <category android:name="android.intent.category.LAUNCHER" />
35             </intent-filter>
36         </activity>
37     </application>
38 
39 </manifest>

这个是不完整的代码 工程的一部分

关键是android 的版本。我原本写的是14到17仅为了配合switcher,但是broadcast却出现了问题。改到8就O.K.了。我的模拟器是4.2.2 真机是4.1.2 SDK是4.2.2
再来么,我发现网上是会以讹传讹的,之前我写的(可以看我注释掉的两部分)一是用了TelephonyManager 其实不必要,因为既然已经用了Broadcast 用context来甄别不同的状态就不用tm了。
另外后面的注释掉部分用了listener 越高越复杂。http://stackoverflow.com/questions/6036295/not-able-to-get-the-telephonymanager-call-state-ringing
另外的话,此应用还碰到一个头疼的问题上面代码没有反映就是连接网络连不上,后来发现是只针对特定端口如3456 改个8080什么的就好了。

 

posted on 2013-03-18 18:25  lauraxia  阅读(890)  评论(0编辑  收藏  举报

导航