作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个 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);
}
}
每次通话结束时,我都会通过 Activity 显示网络状态。但不幸的是,每次通话(拨出/拨入)都显示“未连接!!”。请让我知道问题出在哪里。
我的 list 文件:
<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();
在你的创建 Activity 中
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
}
}
}
在你的 list 文件中添加
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
关于android - OFF_HOOK STATE 永远不会为呼出和呼入调用调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15221242/
我编写了一个 BroadcastReceiver 来检测呼出和呼入的调用。我正在监视用户通话时的网络状态(即当电话状态为摘机时)。不幸的是,来电和去电都不会调用摘机状态。 这是广播接收器: publi
我是一名优秀的程序员,十分优秀!