为什么BroadcastReceiver的onReceive(Context, Intent)执行了两次?

问题描述 投票:0回答:2

我想在断开通话后获取拨打的手机号码的通话详细信息(手机号码、通话时长、日期、时间等)。

到目前为止我做了什么:

我创建一个广播接收器来检测呼叫断开事件。获取通话详细信息后,我从通话记录中获取最新拨打的号码并将其存储在

SQLite
数据库中。

问题是什么:

当我从设备上拨打任何“否”并断开连接时,

onReceive()
方法调用了两次。相同的记录被插入两次。我也通过打印日志进行了检查。 我在谷歌上搜索了这个问题并得到了一些解决方案,例如“仅使用一次
sendBroadcast()
,仅注册广播接收器一次等”。但我不会在任何地方调用
sendBroadcast()
,也不会注册两次。

我是 Android 新手,请建议我做错了什么?

广播接收器:

public class CallReceiver extends BroadcastReceiver {

ContentResolver contentResolver;
Context context;
boolean is_network_roaming = false;
TelephonyManager tm = null;
AppInfo appInfo = null;
ContactHelper contactHelper;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    String TAG = getClass().getSimpleName();
    this.context = context;
    tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    appInfo = new AppInfo(context);
    contactHelper = new ContactHelper(context);


    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

        is_network_roaming = tm.isNetworkRoaming();

        /* Method for getting call details from call log */
        getAllCallLogs();

    }

}

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.callduration"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="22" />

<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.callduration.Splash"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar"
        android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.callduration.MainActivity" >
    </activity>
    <activity android:name="com.callduration.CallHistory" >
    </activity>

    <receiver android:name="com.callduration.receivers.CallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

</manifest>
android broadcastreceiver telephonymanager
2个回答
0
投票

拨打电话时状态变化的顺序是:

CALL_STATE_OFFHOOK--->ACTION_NEW_OUTGOING_CALL-->...{呼叫是否应答 已回答}........................--> CALL_STATE_OFFHOOK-->CALL_STATE_IDLE

根据上述状态,您的 onReceive 应该启动两次,一次是在发起呼叫时,第二次是在呼叫断开时。为了实现您所需要的,即您想要记录单个呼叫会话的事件。您可以使用以下代码,

int state=intent.getStringExtra(TelephonyManager.EXTRA_STATE)
Boolean singlecallstate=false;
switch (state) {
    case TelephonyManager.ACTION_NEW_OUTGOING_CALL:
       singlecallstate=true;
        //any other code you want
    case TelephonyManager.CALL_STATE_IDLE:
       if(singlecallstate){
          getAllCallLogs();
          singlecallstate=false;
 }

0
投票

对于所有参与这个旧讨论的新人:就我而言,问题是我忘记注销接收器

LocalBroadcastManager.getInstance(this@MainActivity).unregisterReceiver(reportManager.testReceiver)
© www.soinside.com 2019 - 2024. All rights reserved.