拨出电话接收一个在andorid中的重定向器,无法发出呼叫

时间:2017-03-03 13:49:25

标签: android android-intent broadcastreceiver

我正在编写一个android重定向调用应用程序,它通过BroadcastReceiver捕获传出的调用并通过服务重定向它们。

但是我找不到我无法接听拨出电话的原因,从未访问onReceive功能。

我的清单(以便您可以看到它不是权限问题)

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

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

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

    <service android:name=".OutgoingCallIntentService" />

</application>

OutgoingCallReceiver.java

    package com.example.outgoingcall;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.widget.Toast;

    public class OutgoingCallReceiver extends BroadcastReceiver {

    private static final String TAG = OutgoingCallReceiver.class.getSimpleName();

    private static boolean mStateOutgoingCall;

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

        if (action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

            if (mStateOutgoingCall) {
                return;
            }

            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            setResultData(null);

            Log.v(TAG, "tel:" + number);
            Toast.makeText(context, "tel:" + number, Toast.LENGTH_SHORT).show();

            Intent intentService = new Intent(context, com.example.outgoingcall.OutgoingCallIntentService.class);
            intentService.setData(Uri.parse("tel:" + number));
            context.startService(intentService);

            mStateOutgoingCall = true;

        } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {

            String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            //Toast.makeText(context, phoneState, Toast.LENGTH_LONG).show();

            Log.v(TAG, "onReceive() " + phoneState);

            if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneState)) {
            }
            else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(phoneState)) {
            }
            else if (TelephonyManager.EXTRA_STATE_IDLE.equals(phoneState)) {
                mStateOutgoingCall = false;
            }
        }

    }

}

OutgoingCallIntentService.java

    package com.example.outgoingcall;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class OutgoingCallIntentService extends IntentService {
    static final public String TAG = OutgoingCallIntentService.class.getSimpleName();

    public OutgoingCallIntentService(String name) {
        super(name);
    }

    public OutgoingCallIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v(TAG, "onHandleIntent()");

        Intent intentActivity = new Intent(Intent.ACTION_CALL);
        intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentActivity.setData(intent.getData());
        startActivity(intentActivity);

    }
}

如果您发现可能存在任何问题,请告诉我。 Thansk

1 个答案:

答案 0 :(得分:0)

发现它!

代码似乎是正确的。

但是,当直接部署到设备时,我需要在手机中设置权限:

应用 - &gt;特定应用 - >电话