【问题标题】:GetFGCallState Always returing Idle stateGetTY allState 总是返回空闲状态
【发布时间】:2013-10-29 06:42:24
【问题描述】:

以下是我的代码,其中我使用反射来访问 com.android.internal.telephony.CallManager。我使用它的函数 GetActiveFGCallState() 来获取不同的状态,例如提醒拨出电话,但它一直让我空闲即使状态是OFF_HOOK或RINGING。所以我从中了解到我的呼叫不在前台,因为如果没有活动的前台呼叫,活动前台呼叫的状态将返回IDLE。那么我的理解正确吗?如果是,那么如何将呼叫活动带到前台,如果不是,那么可能是什么问题?

主要活动代码:

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

}

主要文件:

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


        }

    }

【问题讨论】:

  • 你想要什么,只获取通话状态?对吗?
  • @TheLittleNaruto ...我想知道的是其他人何时接听电话或他/她的手机何时开始响铃...我可以从警报状态中得到什么...所以我想获取警报状态

标签: android reflection call telephonymanager


【解决方案1】:

请尝试以下:

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

Reference.

【讨论】:

  • 我想要这个用于拨出电话而不是拨入电话
  • 伙计,这是用于拨出电话的。如果您要从您的设备拨打电话,它会通知您。试试这个。
  • 我已经尝试过了,为了提供信息,振铃状态不适用于拨出电话。在拨出电话的情况下,只有 OFF_HOOK 和 IDLE 状态有效。按下拨号按钮时会激活 OFF_HOOK 状态当呼叫断开时,空闲状态会被激活。所以要知道您呼叫的人是否接听了您或他/她的手机是否在响铃,您需要使用内部 api 可以通过反射或更改 IDE 文件来使用。I正在使用反射方法访问此 CALLManager API ...剩余在下一条评论中
  • (剩余部分)..我能够访问它的 GetActiveFGCallState() 用于返回状态..这里有关状态的更多详细信息:stackoverflow.com/questions/6290347/… 我面临的问题是它总是返回无论通话状态如何,我都是空闲状态
【解决方案2】:
<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) {}
            }
        }
    }

【讨论】:

  • 这是用来拨出电话的吗?
  • 看到这个函数:onCallStateChanged它会告诉你各种呼叫状态的状态你也可以看到TelephonyManager.Ctrl+空格键以获得更多选项
  • 请阅读我在 TheLittleNaturo 上的 cmets 答案,然后指导我。那是行不通的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 2016-02-07
  • 2015-08-27
  • 2016-04-18
相关资源
最近更新 更多