我已经编写了BroadcastReceiver来检测呼出和呼入。我正在监视用户通话时的网络状态(即电话状态为摘机时)。不幸的是,传入和传出呼叫均未调用off_hook状态。

这是BroadcastReceiver:

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

    }


}


每次通话结束时,我都会通过活动显示网络状态。但是不幸的是,每个呼叫(去电/呼入)都显示“ Not connected !!”。请让我知道问题出在哪里。

我的清单文件:

<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"/>

10-07 12:51