2013-10-29 47 views
3

以下是我使用反射來訪問com.android.internal.telephony.CallManager的代碼。我正在使用它的函數GetActiveFGCallState()來獲取不同的狀態,如Alerting用於傳出呼叫,但它即使狀態爲OFF_HOOK或RINGING,我也會一直返回到閒置狀態。所以我的理解是我的呼叫不在前臺,因爲如果沒有活動的前臺呼叫,主動前臺呼叫的狀態返回IDLE。那麼我的理解是否正確?如果是,那麼如何將呼叫活動帶到前臺,如果沒有,那麼可能是什麼問題?GetFGCallState總是回到空閒狀態

主要活動碼:

package com.example.misscall; 


import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.Menu; 

public class MainActivity extends Activity { 
    final Context context = this; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Intent callIntent = new Intent(Intent.ACTION_CALL, 
       Uri.parse("tel:" + "03365188508")); 
     callIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
     startActivity(callIntent); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

Mainfiest文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.misscall" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 
    <uses-feature android:name="android.hardware.telephony"> 
    </uses-feature> 
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

      <receiver android:name="com.example.misscall.DataConnection"> 
     <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE"/>  
     </intent-filter> 
</receiver> 
     <activity 
      android:name="com.example.misscall.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

數據連接類

import android.os.RemoteException; 
    import android.telephony.PhoneStateListener; 
    import android.telephony.TelephonyManager; 
    import android.util.Log; 
    import android.widget.Toast; 

    public class DataConnection extends BroadcastReceiver { 
     TelephonyManager Tm; 
     ITelephony telephonyService; 
     Class c = null; 
     Method methodGetInstance = null; 
     Method methodGetActiveFgCallState=null; 
     String TAG="Tag"; 
     Object objectCallManager=null; 
     @Override 
     public void onReceive(final Context context, Intent intent) { 

      Tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
      final ClassLoader classLoader = this.getClass().getClassLoader(); 
      try { 
       final Class<?> classCallManager = classLoader.loadClass("com.android.internal.telephony.CallManager"); 
       Log.e(TAG, "CallManager: Class loaded " + classCallManager.toString()); 

       methodGetInstance = classCallManager.getDeclaredMethod("getInstance"); 
       methodGetInstance.setAccessible(true); 
       Log.e(TAG, "CallManager: Method loaded " + methodGetInstance.getName()); 

       objectCallManager = methodGetInstance.invoke(null); 
       Log.e(TAG, "CallManager: Object loaded " + objectCallManager.getClass().getName()); 

       Method[] aClassMethods = classCallManager.getDeclaredMethods(); 
       for(Method m : aClassMethods) 
       { 
        Log.e("MEthods",m.getName()); 
       } 
       methodGetActiveFgCallState = classCallManager.getDeclaredMethod("getActiveFgCallState"); 
       Log.e(TAG, "CallManager: Method loaded " + methodGetActiveFgCallState.getName()); 

       Log.e(TAG, "CallManager: What is the Call state = " + methodGetActiveFgCallState.invoke(objectCallManager)); 
      } 
      catch (ClassNotFoundException e) { 
       Log.e(TAG, e.getClass().getName() + e.toString()); 
      } 
      catch (NoSuchMethodException e) { 
       Log.e(TAG, e.getClass().getName() + e.toString()); 
      } 
      catch (InvocationTargetException e) { 
       Log.e(TAG, e.getClass().getName() + e.toString()); 
      } 
      catch (IllegalAccessException e) { 
       Log.e(TAG, e.getClass().getName() + e.toString()); 
      } 
      Tm.listen(new PhoneStateListener(){ 
       public void onCallStateChanged(int state,String number) { 
        super.onCallStateChanged(state, number); 

        try { 
         if (methodGetActiveFgCallState.invoke(objectCallManager).toString().toLowerCase() .equals("idle")) 
         { 
          Toast.makeText(context, "I am in idle state", Toast.LENGTH_LONG).show(); 
         } 
        } catch (IllegalArgumentException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

       } 

      },PhoneStateListener.LISTEN_CALL_STATE); 


     } 

    } 
+0

你想要什麼,得到的只有通話狀態?對? – TheLittleNaruto

+0

@TheLittleNaruto ...我想知道的是,當其他人接聽電話或當他/她的手機開始響起時...我可以從警戒狀態中獲得...所以我想要獲得那個警戒狀態 – user2137186

回答

2

請在下面試試:

PhoneStateListener callStateListener = new PhoneStateListener() { 
      public void onCallStateChanged(int state, String incomingNumber) 
      { 
        if(state==TelephonyManager.CALL_STATE_RINGING) 
        { 
          Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show(); 

        } 
        if(state==TelephonyManager.CALL_STATE_OFFHOOK) 
        { 
         Toast.makeText(getApplicationContext(),"Phone is Currently in A call", Toast.LENGTH_LONG).show(); 
        } 

        if(state==TelephonyManager.CALL_STATE_IDLE) 
        { 
         Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call", Toast.LENGTH_LONG).show(); 
        } 
      } 
      }; 


telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE); 

希望這會有所幫助。

參考:Here is a link

+0

我想要這對於撥打電話不是來電 – user2137186

+0

男人,這是用於撥出電話。如果您使用設備撥打電話,它會通知您。嘗試這個。 – TheLittleNaruto

+0

我已經試過並且爲了獲取信息,響鈴狀態不適用於撥出電話。只有OFF_HOOK和IDLE狀態在撥出電話的情況下才起作用.OFF_HOOK按下撥號按鈕時激活狀態,當呼叫時激活IDLE狀態斷開連接。爲了知道你打電話的人是否回答了你的問題,或者他/她的手機在響鈴,你需要使用內部api,這可以通過反射或改變IDE文件來使用。我使用反射方法訪問這CALLManager API ...留在下一個評論 – user2137186

1
<receiver android:name=".OutgoingCallReceiver"> 
     <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE"/>  
     </intent-filter> 
</receiver> 



    public class OutgoingCallReceiver extends BroadcastReceiver { 
     private static long timeStarted = -1L; // IMPORTANT! 

     private static String number; 
     private static boolean noCallListenerYet = true; 

     @Override 
     public void onReceive(final Context context, Intent intent) { 
      PhoneCallListener phoneListener = new PhoneCallListener(context); 
      TelephonyManager telephonyManager = (TelephonyManager) context 
        .getSystemService(Context.TELEPHONY_SERVICE); 
      telephonyManager.listen(phoneListener, 
        PhoneStateListener.LISTEN_CALL_STATE); 
     } 

     private class PhoneCallListener extends PhoneStateListener { 
      Context context; 
      private boolean isPhoneCalling = false; 

      public PhoneCallListener(Context context2) { 
       // TODO Auto-generated constructor stub 
       context=context2; 
      } 

      @Override 
      public void onCallStateChanged(int state, String incomingNumber) { 
       if (state == TelephonyManager.CALL_STATE_OFFHOOK) { 
       //do something here 
       } 

       if (state == TelephonyManager.CALL_STATE_IDLE && timeStarted != -1L) {} 
      } 
     } 
    } 
+0

是這是爲了呼出電話? – user2137186

+0

試試這個曾經.. – KOTIOS

+0

看到這個函數:onCallStateChanged它會告訴ü各種呼叫的狀態狀態ü還可以看到TelephonyManager.Ctrl +空格鍵以獲得更多選項 – KOTIOS

相關問題