0

I am doing one android application, for that I used com.android.internal API's. They are Call.java, CallManger.java. For these classes I created subclass for Call.java. You can find these two classes here http://hi-android.info/src/com/android/internal/telephony/Call.java.html and http://hi-android.info/src/com/android/internal/telephony/CallManager.java.html.

Subclass of Call.java is:

public class MyCall extends Call{

 1.     CallManager cm = CallManager.getInstance(); 
 2.     Phone.State state;
 3.     
 4.      Phone mDefaultPhone;
 5.      private final ArrayList<Connection> emptyConnections = new ArrayList<Connection>();    
 6.      List<Call> ringingCall;
 7.     
 8.     @Override
 9.     public List<Connection> getConnections() {      
 10.        
 11.        
 12.        state = cm.getState();      
 13.        ringingCall = cm.getForegroundCalls();     
 14.        System.out.println("**inside getConnections="+state);   
 15.        System.out.println("**inside getConnections="+ringingCall); 
 16.        if ( ringingCall == null) {
 17.            
 18.            System.out.println("**call is null***");
 19.            return emptyConnections;
 20.         }
 21.         else
 22.         {
 23.            System.out.println("**call is not null***");
 24.            return ((Call) ringingCall).getConnections();   
            }


        }

        @Override
        public Phone getPhone() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void hangup() throws CallStateException {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean isMultiparty() {
            // TODO Auto-generated method stub
            return false;
        }

        public Connection
        getEarliestConnection() {
            List l;
            long time = Long.MAX_VALUE;
            Connection c;
            Connection earliest = null;

            l = getConnections();

            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;
        }
    }

I called instance of CallManger Like this:

 CallManager cm = CallManager.getInstance();

Bcoz this is a final modifier class. My another class is CallUpdate.java.

public class CallUpdate {   

        Call myCall = new MyCall();
        Connection myConn = new MyConnection();
        CallManager cm = CallManager.getInstance();

            public Object getCallFailedString(){

               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);
                  }
          }

I am getting myConn value is null. For this I added some piece of code in getConnection method of MyCall class to get a non-null value of myConn. i.e

 state = cm.getState();     
        ringingCall = cm.getForegroundCalls();     
        System.out.println("**inside getConnections="+state);   
        System.out.println("**inside getConnections="+ringingCall); 
        if ( ringingCall == null) {

            System.out.println("**call is null***");
            return emptyConnections;
        }
        else
        {
            System.out.println("**call is not null***");
            return ((Call) ringingCall).getConnections();   
        }

But it is throwing ClassCastException error on Line No:24 and on line l = getConnections();. And also I need at least one outgoing call to get a value of myConn. How to resolve this error?

Thx in advance.

1 Answer 1

1

ringingCall is a List<Call> - not a single call. You probably want something like:

if (ringingCall != null && !ringingCall.isEmpty()) {
    Call call = ringingCall.get(0);
    // Use call
}

... but you should also consider what you want to do if there's more than one call.

In general, if you find yourself casting that's because you think you know better than the compiler. It should always at least make you think - and in the case where you want a Call but you've got a List<Call>, the natural approach should be to use an element from the list.

It's not clear whether you really want to be subclassing Call in the first place, to be honest.

Sign up to request clarification or add additional context in comments.

5 Comments

What I have to do to get an active Call in phone. Can u give me any suggestions.
@shiv1229: Empty call, or an empty list? Are you calling that method while a call is in progress? Note that the fact that it's an internal API suggests this may not be the best approach, to be honest...
Sorry empty list. pLZ tel me which is the best approach. Actually I want to get Outgoing call state's. (i.e Busy, Not-reachable and power-off) of destination number.
Yes I am calling that method while a call in progress .Bcoz I want a non-null Connection value.
@shiv1229: Then I don't know why the list is empty - but as I say, using an internal API is rarely a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.