【问题标题】:Interrupt incoming call in android在android中中断来电
【发布时间】:2014-01-21 08:20:27
【问题描述】:

如何在来电时打开带有接听和拒绝按钮的自定义 UI,我想显示自定义 UI 而不是默认拨号器。 我正在使用以下代码,但拨号器已打开且我的活动未打开:

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

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>
    <activity android:name=".IncomingCall"></activity>

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

我有一个广播接收器,它监听来电,接收器是:

@Override
public void onReceive(Context context, Intent intent) {
       Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: ");

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: " + state);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            Intent i = new Intent(context, IncomingCall.class);
            i.putExtras(intent);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(i);
        }
}

IncomingCall 类的代码是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

    setContentView(R.layout.activity_main);

    String number = getIntent().getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    TextView text = (TextView)findViewById(R.id.text);
    text.setText("Incoming call from " + number);
}

但我的自定义 UI 没有显示。

我还想要我的 UI 上的按钮以及如何通过单击该按钮来接听来电。 提前致谢。

编辑:-- 更新我的代码后,我可以打开我的自定义 UI,现在我想通过单击其上的按钮来接听电话。如何做到这一点任何帮助..

【问题讨论】:

  • 可能您的设备已选择默认拨号器以始终接听来电。进行网络搜索,了解如何为此清除默认应用。
  • 我在模拟器上测试它,当我运行应用程序时,它显示拨号器,但当我调试应用程序时,它显示我的自定义 ui。所以我错过了。
  • 检查此 [link][1]、[link1][2] 和 [link][3] [1]:stackoverflow.com/questions/9256218/… [2]:stackoverflow.com/questions/8699257/… [3]:@ 987654323@

标签: android telephonymanager phone-call


【解决方案1】:

首先执行以下操作:

try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

然后在调用您的活动之前调用abortBroadcast() 方法,并确保在您的intentFilter 中设置优先级,如下所示:

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

要从自定义 UI 接听来电,请执行以下操作:

answerButton = (Button) findViewById(R.id.pickup);
    answerButton.setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
            answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,   KeyEvent.KEYCODE_HEADSETHOOK));
     context.sendOrderedBroadcast(answer, null);

        }
    });

要拒绝来电,请执行以下操作:

rejectButton= (Button) findViewById(R.id.pickup);
    rejectButton= .setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
    buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
    getApplicationContext().sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");


    }
});

希望对你有帮助

【讨论】:

  • 尝试了您的代码,但在来电时仍然打开相同的默认拨号器
  • 在使用您的代码并在调用我的自定义 UI 之前添加延迟后,我能够显示我的 UI,现在我如何通过单击按钮接听电话。
  • 这是一个新问题 :) 请给我一个正确答案并开始一个新问题。
  • 添加了你的代码在添加延迟后不起作用,它对我有用。或者我可以说你的答案不完整。这不是一个新问题,这也是我最初问题的一部分.
  • 我尝试了您的代码,并在通过单击按钮接听电话时发现异常。异常是 -android.content.ActivityNotFoundException:未找到处理 Intent { act=android.intent.action.ANSWER flg= 的活动0x10000000 }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
相关资源
最近更新 更多