Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BroadcastReceiver onReceive()在Android 5.1.1上调用了两次,即使在一次注册之后也是如此_Android_Broadcastreceiver_Android Broadcastreceiver - Fatal编程技术网

Android BroadcastReceiver onReceive()在Android 5.1.1上调用了两次,即使在一次注册之后也是如此

Android BroadcastReceiver onReceive()在Android 5.1.1上调用了两次,即使在一次注册之后也是如此,android,broadcastreceiver,android-broadcastreceiver,Android,Broadcastreceiver,Android Broadcastreceiver,我无法找出下面代码的错误。我还检查了两次关于注册接收者的情况。但事实并非如此。也许我错过了什么。 请帮忙。我真的需要它( 下面是我从中调用它的活动 public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout

我无法找出下面代码的错误。我还检查了两次关于注册接收者的情况。但事实并非如此。也许我错过了什么。 请帮忙。我真的需要它(

下面是我从中调用它的活动

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>


事实上……代码没有问题。是系统触发的。有时是2次,有时是4次


因此,我们最好在代码中处理它。

正如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);
            }
        }
    }

我也有同样的行为,您的代码中没有问题。android.intent.action.PHONE_状态可以接收几次,但intent的捆绑包总是不同的,例如:

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

对于这两个操作中的哪一个,它会被调用两次?这可能是因为系统会发送两次。当您收听呼叫或联系人的广播时,这实际上是正确的。它的Toast.makeText(…)每次打电话或打电话我都会打两次。我还没查到那么多。但是你听到的是两次电话状态还是两次外呼?@Arslan所有的祝酒词…例如2个“外呼”,2个“外呼”,2个“接听”,2个“断开”。但是,如果状态为传入sms,则此代码将失败。如果用户希望执行两次操作,例如快进音频30秒,则第二次快进将不起作用,因为最后一个状态与当前状态相同。在这种情况下,您可以传递另一个参数。
D/App(2001): Bundle{ state => OFFHOOK; subscription => 1; }Bundle
D/App(2001): Bundle{ incoming_number => 555555555; state => OFFHOOK; subscription => 9223372036854775807; }Bundle