在我的项目中,我只使用了一个类,即扩展广播接收器(只检查是否被调用了两次)。没有其他类文件。因此,从其他地方多次调用它是没有问题的。我已经把它声明如下-
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一旦呼叫连接时被呼叫,当呼叫终止时(我通过注意时间戳得出结论),广播接收机在检测来电时的行为是否正常?总要叫两次吗?还是我漏掉了什么?
发布于 2014-08-24 10:06:49
onReceive被称为getting是因为:
如果您只想在电话铃响时执行某些操作,那么您必须在onReceive中这样做:
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state=TelephonyManager.CALL_STATE_RINGING)[
//do something here
}https://stackoverflow.com/questions/25470373
复制相似问题