Android在广播接收器之间传递数据

时间:2018-04-02 19:51:13

标签: android broadcastreceiver

我正在创建一个记录通话的应用。我创建了2个接收器,一个用于收听电话状态,另一个用于拨打电话号码。我可以检测到来自第一个接收器的拨出电话。但我无法从第一个接收器获得拨出电话号码。所以我不得不创建一个第二个接收器来处理从我可以获得电话号码的拨出电话。但我想将来电号码从第二接收方发送给第一接收方。我怎么做?我试过通过Intent发送广播,但我得到了一个例外。我可以通过其他任何方式将数据从一个接收器发送到另一个接收器吗?

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.js.globemaster.autocallrecorder">

    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".IncomingCallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
            </intent-filter>
        </receiver>

        <receiver android:name=".OutgoingCallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

OutgoingCallReceiver.java

public class OutgoingCallReceiver extends BroadcastReceiver {
    String phonenumber;
    private SaveObject saveObject;

    @Override
    public void onReceive(Context context, Intent intent) {
        phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        saveObject = SaveObject.getInstance();
        saveObject.setOutgoingnumber(phonenumber);
        Log.e("Outgoing", phonenumber);

    }
}

IncomingReceiver.java:

public class IncomingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

        } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

只需使用下一个CallReceiver创建一个intent-filter

<receiver
        android:name=".CallReceiver"
        android:enabled="true"
        android:exported="true">

        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
</receiver>

在你的CallReceiver尝试这样做:

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (ACTION_NEW_OUTGOING_CALL.equals(action)) {
        // It is outgoing call
        String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        // You can save this phoneNumber in sharedPreferences for further using
    } else if (ACTION_PHONE_STATE_CHANGED.equals(action)) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        // Get here your phoneNumber from sharedPreferences and use it

        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

        } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

        }
    }
}