0

我是 android 的初学者,我正在开发一个应用程序,只要用户拨打电话,BrocastReceiver 就会启动...我正在尝试监视广播接收器内的网络状态...并且我正在尝试在用户启动活动挂断电话...但不幸的是它给了我一个错误...请让我知道该怎么做...

当用户拨打电话时,我的广播接收器正常启动......但我不知道如何在用户通话时监控 BroadcastReceiver 内的网络......并在用户挂断电话时从 BroadcastReceiver 调用活动。 ..

请让我知道如何实现这一目标......提前致谢...... :-)

我的代码...

  1. 广播接收器代码

     public class MoniterNetworkStatus extends BroadcastReceiver{
    
    private ConnectivityManager connectivityManager;
    private NetworkInfo info;
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        Toast.makeText(context, "Monitering Network Status !!!", Toast.LENGTH_LONG).show();
    
        boolean networkStatus = false;
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
    
        Intent activityIntent = new Intent(context, NetworkStatusActivity.class);
    
                // I am monitoring the network status as long as user is on call...
        while(telephonyManager.getCallState()!= TelephonyManager.CALL_STATE_IDLE){
    
            info = connectivityManager.getActiveNetworkInfo();
            if(info == null){
                networkStatus =  false;
            }else if(info.getType() == ConnectivityManager.TYPE_MOBILE){
    
                networkStatus = info.isConnected();
            }else{
                networkStatus = false;
            }
        }
    
    
    
        //Putting the network details in to the activity.   
        activityIntent.putExtra("operatorName", telephonyManager.getNetworkOperatorName());
        activityIntent.putExtra("networkStatus", networkStatus);
    
                // starting the activity
        context.startActivity(activityIntent);
        abortBroadcast();
    
        }
    }
    

    我的清单文件

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    
     <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.INTERNET"/> -->
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    
         <activity android:name="com.abc.activity.NetworkStatusActivity"></activity>
    
        <receiver android:name="com.abc.broadcastreceiver.MoniterNetworkStatus"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <action android:name="android.intent.action.ANSWER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    
    </application>
    

当用户拨打电话时,我的广播接收器正常启动......但我不知道如何在用户通话时监控网络......并在用户挂断电话时调用活动......

请让我知道如何实现这一目标......提前致谢...... :-)

4

1 回答 1

1

您需要注册另一个接收器以接受 ACTION_PHONE_STATE_CHANGED。这可能是Detecting去电和通话挂断事件在android中的重复

于 2013-02-28T10:59:01.887 回答