How to get a Call state of a phone [duplicate]

2019-08-29 08:28发布

问题:

Possible Duplicate:
Trouble with reading phone state

I want to get a call state of phone in my application. I am doing one application in which , When user dial to a number, then my application should come to know whether other side phone is Busy, Not-reachable or power-off states etc. For this I used com.android.internal API's they are Call.java<http://hi-android.info/src/com/android/internal/telephony/Call.java.html>, CallManger.java<http://hi-android.info/src/com/android/internal/telephony/CallManager.java.html> and Connection.java<http://hi-android.info/src/com/android/internal/telephony/Connection.java.html>. I created subclass of Call.java like this:

public class MyCall extends Call{   

  CallManager cm = CallManager.getInstance();   
  Connection c;
  Phone mDefaultPhone;
  private final ArrayList<Connection> emptyConnections = new ArrayList<Connection>();
  int size; 
  List<Call> ringingCall = cm.getForegroundCalls();

  @Override
  public List<Connection> getConnections() {             
  System.out.println("**value of list***"+ringingCall.size());      
     if(ringingCall != null && !ringingCall.isEmpty()){             
     System.out.println("inside if****");
     System.out.println("**call is not null***");
     System.out.println("value of call"+ringingCall);
     return  ((MyCall) ringingCall).getConnections();
   }
   else{
      System.out.println("**list is  null***");
          return emptyConnections;
    }       
  }
  @Override
  public Phone getPhone() {
   // TODO Auto-generated method stub
  System.out.println("**inside getPhone***");
  return null;
 }
 @Override
 public void hangup() throws CallStateException {
 // TODO Auto-generated method stub
  System.out.println("**inside hangUp***");
 }
 @Override
 public boolean isMultiparty() {
  // TODO Auto-generated method stub
  System.out.println("**inside isMultiparty***");
  return false;
 }
 public Connection
   getEarliestConnection() {
    Connection myConn = new MyConnection();
    System.out.println("inside EarliestConnection"); 
     List<Connection> l;
     long time = Long.MAX_VALUE;
     Connection c;
     Connection earliest = null;
     l = getConnections();
     System.out.println("value of connection is=="+l); 
     if (l == null) {
        return null;
      }else if ( l.size() == 0)
      {
         return null;
        }
     for (int i = 0, s = l.size() ; i < s ; i++) {
     c = (Connection) l.get(i);
     long t;
     t = c.getCreateTime();
     if (t < time) {
       earliest = c;
       time = t;
     }
   }
  return earliest;
  }
}

Here I am getting the ringingCall.size is 0 for that it is executing else part. And one more class is CallUpdate.java.

public class CallUpdate {   

   Call myCall = new MyCall();   
   CallManager cm = CallManager.getInstance();
   public Object getCallFailedString(){

     Connection myConn = myCall.getEarliestConnection();
     System.out.println("myConn is  ******"+myConn);
     System.out.println("myCall is  ******"+myCall);    
     if(myConn == null){
        System.out.println("myConn is null ******");
        return null;
     }              
      else
        {
      Connection.DisconnectCause cause = myConn.getDisconnectCause();                        
       System.out.println("myconn is not null ******"+cause);   
        switch(cause){
             case BUSY :
                System.out.println("inside busy");
          break;
        case NUMBER_UNREACHABLE :
           System.out.println("inside un-reachable");
         break;
        case POWER_OFF :
           System.out.println("inside power off");
         break;  
          }         

        }
    return myConn;
 }

}

In this code getting myConn value is null. I called CallManger class is like this:

CallManager cm = CallManager.getInstance();

But when I print this getting null pointer exception. System.out.println("value of cm"+cm); why this exception? Can anybody tell me?

回答1:

You cannot obtain call related info like user busy, phone switch off etc directly by accessing internal API. Android does not provide these details for security reasons. All you can do is trace the Log. we have three kinds of logs, so you have to trace "Radio Log" only.

It consist of solcited and unsolicited commands, which are nothing but instructions sent from phone modem to Android OS and vice versa.

There you will find the line: onDisconnect Cause: XXXX which will be answer to your question.



回答2:

you really should take a look at the code in PhoneApp.java. tracing this line, you will find out that 'cm = CallManager.getInstance()' is useless . the key to this problem is GsmCall.java and GsmCallTracker