Android 对于呼出和呼入呼叫,从不调用摘机状态

Android 对于呼出和呼入呼叫,从不调用摘机状态,android,android-activity,broadcastreceiver,telephonymanager,Android,Android Activity,Broadcastreceiver,Telephonymanager,我已经编写了一个广播接收器来检测呼出和呼入的呼叫。当用户处于通话状态时(即当手机状态为断开连接时),我会监控网络状态。不幸的是,对于传入和传出呼叫,未调用off_hook状态 这是广播接收机: public class BroadcastReceiverImpl extends BroadcastReceiver{ private ConnectivityManager connectivityManager; private TelephonyManager telephon

我已经编写了一个广播接收器来检测呼出和呼入的呼叫。当用户处于通话状态时(即当手机状态为断开连接时),我会监控网络状态。不幸的是,对于传入和传出呼叫,未调用off_hook状态

这是广播接收机:

public class BroadcastReceiverImpl extends BroadcastReceiver{

    private ConnectivityManager connectivityManager;
    private TelephonyManager telephonyManager;
    private NetworkInfo networkInfo;
    private String networkStauts = "Not connected !!";

    //java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime().
    @Override
    public void onReceive(final Context context, Intent intent) {

        Toast.makeText(context, "Inside onReceive Method !!", Toast.LENGTH_SHORT).show();

        connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = connectivityManager.getActiveNetworkInfo();

        final Intent activityIntent = new Intent(context, ActivityImpl.class);

        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneStateListener(){

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {


                switch (state) {

                case TelephonyManager.CALL_STATE_RINGING:
                     Toast.makeText(context, "Call State Ringing !!", Toast.LENGTH_SHORT).show();   
                    break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Toast.makeText(context, "Call State Offhook !!", Toast.LENGTH_SHORT).show();
                    while (networkInfo!=null && networkInfo.isConnectedOrConnecting()) {
                        networkStauts = "Network Connected";
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;

                case TelephonyManager.CALL_STATE_IDLE:
                    Toast.makeText(context, "Call State Idle !!", Toast.LENGTH_SHORT).show();
                    activityIntent.putExtra("networkStatus", networkStauts);
                    activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(activityIntent);
                    abortBroadcast();
                    break;
                default:
                    break;
                }

                super.onCallStateChanged(state, incomingNumber);
            }   




        }, PhoneStateListener.LISTEN_CALL_STATE);

    }


}
每次通话结束时,我都会通过活动显示网络状态。但不幸的是,对于每个呼叫(传出/传入),它都显示“未连接!!”。请告诉我是什么问题

我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.monitornetwork_v10"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

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

        <activity android:name="com.example.activity.NetworkMonitorActivityImpl"></activity>

        <receiver
            android:name="com.example.broadcastreceiver.BroadcastReceiverImpl"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_PHONE_STATE_CHANGED" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.intent.action.ANSWER" />
                <action android:name="android.intent.action.PHONE_STATE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

您应该扩展PhoneStateListener

TelephonyManager mTelManager;
final MyPhoneStateListener mPhoneStateListener = new MyPhoneStateListener();
在您的活动onCreate中

mTelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
创建一个内部类

protected class MyPhoneStateListener extends PhoneStateListener
{
    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        switch (state)
                {
                    case TelephonyManager.CALL_STATE_RINGING:

                       // do whatever you want here
                        break;

                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        // do whatever you want here
                        break;

                    case TelephonyManager.CALL_STATE_IDLE:
                        // do whatever you want here
                }

    }
}
在清单文件中添加

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

您应该扩展PhoneStateListener

TelephonyManager mTelManager;
final MyPhoneStateListener mPhoneStateListener = new MyPhoneStateListener();
在您的活动onCreate中

mTelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
创建一个内部类

protected class MyPhoneStateListener extends PhoneStateListener
{
    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        switch (state)
                {
                    case TelephonyManager.CALL_STATE_RINGING:

                       // do whatever you want here
                        break;

                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        // do whatever you want here
                        break;

                    case TelephonyManager.CALL_STATE_IDLE:
                        // do whatever you want here
                }

    }
}
在清单文件中添加

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


Thank Hoan Nguyen:-)它现在将进入摘机状态,但摘机状态内的while循环未被执行。您认为原因是什么?将您的网络状态记录在onCreate中,以查看它是否已连接。您的网络可能从一开始就没有连接。谢谢Hoan Nguyen:-)它现在将进入摘机状态,但是摘机状态中的while循环没有执行。您认为原因是什么?将您的网络状态记录在onCreate中,以查看它是否已连接。您的网络甚至可能从一开始就没有连接。