Android 5.1.1上即使注册一次,BroadcastReceiver的onReceive()仍会被调用两次。

5

我无法弄清楚下面的代码有什么问题。我还检查了一下注册接收器两次的情况,但也不是这个问题。或者可能是我漏掉了什么。 请问有人能帮忙吗?我真的很需要。:(

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * @author Bharat
 *
 */
public class CallNotifierService extends Service 
{
    private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
    private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL";
    private CallBr br_call;

    @Override
    public IBinder onBind(Intent arg0) 
    {
        return null;
    }

    @Override
    public void onDestroy() 
    {
        Log.d("service", "destroy");
        this.unregisterReceiver(this.br_call);
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        final IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_OUT);
        filter.addAction(ACTION_IN);
        this.br_call = new CallBr();
        this.registerReceiver(this.br_call, filter);
        return START_NOT_STICKY;
    }

    public class CallBr extends BroadcastReceiver 
    {
        Bundle bundle;
        String state;
        String inCall, outCall;
        public boolean wasRinging = false;
        public boolean answered = false;
        public boolean outgoing = false;

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            if (intent.getAction().equals(ACTION_IN)) 
            {
                if ((bundle = intent.getExtras()) != null) 
                {
                    state = bundle.getString(TelephonyManager.EXTRA_STATE);

                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
                    {
                        inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                        wasRinging = true;
                        Toast.makeText(context, "Incoming Call : " + inCall, Toast.LENGTH_LONG).show();
                    } 
                    else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
                    {
                        if (wasRinging == true) 
                        {
                            answered = true;
                            Toast.makeText(context, "Answered", Toast.LENGTH_LONG).show();
                        }
                    } 
                    else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) 
                    {
                        wasRinging = false;
                        Toast.makeText(context, "Disconnected", Toast.LENGTH_LONG).show();
                    }
                }
            } 
            else if (intent.getAction().equals(ACTION_OUT)) 
            {
                if ((bundle = intent.getExtras()) != null) 
                {
                    outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                    Toast.makeText(context, "Outgoing Call : " + outCall, Toast.LENGTH_LONG).show();
                    outgoing = true;
                }
            }
        }
    }
}

以下是我正在调用的活动。
public class MyActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.roaming);

    Intent intent = new Intent(this, CallNotifierService.class);
    startService(intent);
}
.
.
.
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ind.cosmos.main"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <!-- android:theme="@style/AppTheme"> -->
        <activity
            android:name="ind.cosmos.main.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="ind.cosmos.callRecord.CallNotifierService" />
    </application>


这两个操作中,哪一个被调用了两次? - Yash
每次接听电话或拨打电话时,Toast.makeText(...)会被调用两次 - Bharat
是的,我放置了这个日志。Log.i("Registerd Once",this.br_call.toString()); 然而,当Toast显示两次时,它只打印一次。 ===LOG===> 02-18 17:12:12.485: I/Registerd Once(23513): ind.cosmos.callRecord.CallNotifierService$CallBr@2eeef552 - Bharat
当我配对蓝牙设备时,我遇到了类似的问题。BroadcastReceiver会两次调用,并显示“配对完成”的信息。 - Brian Reinhold
在这里回答 - https://dev59.com/g2Ei5IYBdhLWcg3wZ7ka#46563020 - Rasool Mohamed
显示剩余5条评论
3个回答

7

正如 Bharat 所说,最好在代码中进行处理。以下是一个技巧,可以在 这里 找到,感谢 Michael Marvick 提供的详细说明。

     public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

        public static final String TAG = "PHONE STATE";
        private static String mLastState;

        @Override
        public void onReceive(Context context, Intent intent) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

            if (!state.equals(mLastState)) {
                mLastState = state;
                Log.e(TAG, state);
            }
        }
    }

但是,如果状态是传入短信,那么这段代码将失败。 - TAHA SULTAN TEMURI
如果用户想要执行两次某个操作,例如快进音频30秒,第二次快进将不起作用,因为最后状态与当前状态相同。 - itabdullah
在这种情况下,您可以传递另一个参数。 - Chitrang

5

实际上.. 代码没有问题。是系统触发了这个问题。有时是2次,有时是4次。

所以最好的解决方法就是在代码中处理它。


0

我有相同的行为,你的代码没有问题。android.intent.action.PHONE_STATE 可以接收几次,但意图的 bundle 总是不同的,例如:

D/App(2001): Bundle{ state => OFFHOOK; subscription => 1; }Bundle
D/App(2001): Bundle{ incoming_number => 555555555; state => OFFHOOK; subscription => 9223372036854775807; }Bundle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接