我编写了一个 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);
}
}
每次通话结束时,我都会通过活动显示网络状态。但不幸的是,每次通话(呼出/呼入)都显示“未连接!!”。请让我知道问题所在。
我的清单文件:
<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>