从广播接收器调用活动

时间:2013-02-28 10:54:46

标签: android android-activity android-networking android-broadcast

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

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

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

我的代码......

  1. BroadcastReceiver代码

     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>
    

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

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

1 个答案:

答案 0 :(得分:1)

您需要注册另一个接收器才能接受ACTION_PHONE_STATE_CHANGED。这可能是Detecting outgoing call and call hangup event in android

的副本