我只在我的项目中使用了一个扩展广播接收器的类(仅检查它是否被调用两次)。没有其他类文件。所以毫无疑问,从其他地方多次调用它。我在下面的清单中声明了它 -
的Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactlistview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".Detection" >
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Detection.java
public class Detection extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.i("Called", "OnReciver");
Toast.makeText(context, "incoming", Toast.LENGTH_LONG).show();
}
}
日志输出 -
08-24 14:50:21.707: I/Called(21758): OnReciver
08-24 14:50:32.648: I/Called(21758): OnReciver
一旦呼叫连接时呼叫,一旦呼叫终止呼叫(我通过记下时间戳得出结论) 检测到来电时,广播接收器是否正常?它总是被叫两次?或者我错过了什么?
答案 0 :(得分:0)
onReceive被称为twiced因为:
- 电话响铃时:EXTRA_STATE_RINGING
- 完成响铃后手机处于空闲状态时:EXTRA_STATE_IDLE
醇>
如果您只想在电话响铃时执行某些操作,那么您必须在onReceive中执行此操作:
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state=TelephonyManager.CALL_STATE_RINGING)[
//do something here
}